Javascript 用动态图绘制叠层图
我想使用JavaScript库绘制一个堆叠图,我在下面已经完成了。但是我希望每一条线都有不同的颜色。两种颜色,如深色的条形顶部和浅色的底部,如下所示: 请建议如何为动态图像传递不同的颜色。Javascript 用动态图绘制叠层图,javascript,dygraphs,Javascript,Dygraphs,我想使用JavaScript库绘制一个堆叠图,我在下面已经完成了。但是我希望每一条线都有不同的颜色。两种颜色,如深色的条形顶部和浅色的底部,如下所示: 请建议如何为动态图像传递不同的颜色。 以下是我目前掌握的情况: 功能条形图绘图仪(e){ var ctx=e.drawingContext; var点=e点; var y_bottom=e.dygraph.toDomYCoord(0);//请参见 //这实际上应该以最小间隙为基础 变量栏宽度=2/3*(点[1]。canvasx-点[0]。ca
以下是我目前掌握的情况:
功能条形图绘图仪(e){
var ctx=e.drawingContext;
var点=e点;
var y_bottom=e.dygraph.toDomYCoord(0);//请参见
//这实际上应该以最小间隙为基础
变量栏宽度=2/3*(点[1]。canvasx-点[0]。canvasx);
ctx.fillStyle=e.color;//浅色可能更美观
//进行实际绘图。
对于(变量i=0;i
/*
相关CSS类包括:
X轴和Y轴上的标签:
.动态图轴标签
.dygraph-axis-label-x
.dygraph-axis-label-y
.dygraph-axis-label-y2
图表标签,请参见http://dygraphs.com/tests/styled-chart-labels.html
.动态图标题
.dygraph xlabel
.DYLABEL
.dygraph-y2label
图例,请参见http://dygraphs.com/tests/series-highlight.html
.动态图表图例
*/
.动态图标题{
颜色:灰色;
}
以下是添加不同颜色的代码:
<div id="graph"></div>
function barChartPlotter(e) {
var myColor = ["#ECD078", "#D95B43", "#C02942", "#542437", "#53777A", "#42d7f4", "red", "green"];
var ctx = e.drawingContext;
var points = e.points;
var y_bottom = e.dygraph.toDomYCoord(0);
// This should really be based on the minimum gap
var bar_width = 2 / 3 * (points[1].canvasx - points[0].canvasx);
// a lighter shade might be more aesthetically pleasing
// Do the actual plotting.
for (var i = 0; i < points.length; i++) {
var p = points[i];
var center_x = p.canvasx; // center of the bar
ctx.fillStyle = myColor[i];
ctx.fillRect(center_x - bar_width / 2, p.canvasy, bar_width, y_bottom - p.canvasy);
ctx.strokeRect(center_x - bar_width / 2, p.canvasy, bar_width, y_bottom - p.canvasy);
}}
g = new Dygraph(document.getElementById("graph"),
"X,Y\n" +
"1,2\n" +
"2,4\n" +
"3,6\n" +
"4,8\n" +
"5,10\n" +
"6,12\n" +
"7,14\n" +
"8,16\n", {
// options go here. See http://dygraphs.com/options.html
legend: 'always',
animatedZooms: true,
plotter: barChartPlotter,
dateWindow: [0, 9]
});
功能柱状图绘图仪(e){
var myColor=[“ECD078”、“D95B43”、“C02942”、“542437”、“53777A”、“42d7f4”、“红色”、“绿色”];
var ctx=e.drawingContext;
var点=e点;
变量y_bottom=e.dygraph.toDomYCoord(0);
//这实际上应该以最小间隙为基础
变量栏宽度=2/3*(点[1]。canvasx-点[0]。canvasx);
//浅色可能更美观
//进行实际绘图。
对于(变量i=0;i
以下是添加不同颜色的代码:
<div id="graph"></div>
function barChartPlotter(e) {
var myColor = ["#ECD078", "#D95B43", "#C02942", "#542437", "#53777A", "#42d7f4", "red", "green"];
var ctx = e.drawingContext;
var points = e.points;
var y_bottom = e.dygraph.toDomYCoord(0);
// This should really be based on the minimum gap
var bar_width = 2 / 3 * (points[1].canvasx - points[0].canvasx);
// a lighter shade might be more aesthetically pleasing
// Do the actual plotting.
for (var i = 0; i < points.length; i++) {
var p = points[i];
var center_x = p.canvasx; // center of the bar
ctx.fillStyle = myColor[i];
ctx.fillRect(center_x - bar_width / 2, p.canvasy, bar_width, y_bottom - p.canvasy);
ctx.strokeRect(center_x - bar_width / 2, p.canvasy, bar_width, y_bottom - p.canvasy);
}}
g = new Dygraph(document.getElementById("graph"),
"X,Y\n" +
"1,2\n" +
"2,4\n" +
"3,6\n" +
"4,8\n" +
"5,10\n" +
"6,12\n" +
"7,14\n" +
"8,16\n", {
// options go here. See http://dygraphs.com/options.html
legend: 'always',
animatedZooms: true,
plotter: barChartPlotter,
dateWindow: [0, 9]
});
功能柱状图绘图仪(e){
var myColor=[“ECD078”、“D95B43”、“C02942”、“542437”、“53777A”、“42d7f4”、“红色”、“绿色”];
var ctx=e.drawingContext;
var点=e点;
变量y_bottom=e.dygraph.toDomYCoord(0);
//这实际上应该以最小间隙为基础
变量栏宽度=2/3*(点[1]。canvasx-点[0]。canvasx);
//浅色可能更美观
//进行实际绘图。
对于(变量i=0;i
以下是为同一条添加两种不同颜色的代码:
<div id="graph"></div>
function barChartPlotter(e) {
var myColor = ["#ECD078", "#D95B43", "#C02942", "#542437", "#53777A", "#42d7f4", "red", "green"];
var myColor1 = ["black", "red", "green", "#ECD078", "#D95B43", "#C02942", "#542437", "#53777A"];
var ctx = e.drawingContext;
var points = e.points;
var y_bottom = e.dygraph.toDomYCoord(0);
// This should really be based on the minimum gap
var bar_width = 2 / 3 * (points[1].canvasx - points[0].canvasx);
// a lighter shade might be more aesthetically pleasing
// Do the actual plotting.
for (var i = 0; i < points.length; i++) {
var p = points[i];
var center_x = p.canvasx; // center of the bar
ctx.fillStyle = myColor[i];
ctx.fillRect(center_x - bar_width / 2, p.canvasy, bar_width, y_bottom - p.canvasy);
ctx.strokeRect(center_x - bar_width / 2, p.canvasy, bar_width, y_bottom - p.canvasy);
}
for (var i = 0; i < points.length; i++) {
var p = points[i];
console.log(points[1]);
var center_x = p.canvasx; // center of the bar
ctx.fillStyle = myColor1[i];
ctx.fillRect(center_x - bar_width / 2, p.canvasy, bar_width, (y_bottom - p.canvasy) / 2);
ctx.strokeRect(center_x - bar_width / 2, p.canvasy, bar_width, (y_bottom - p.canvasy) / 2);
}
}
g = new Dygraph(document.getElementById("graph"),
"X,Y\n" +
"1,2\n" +
"2,4\n" +
"3,6\n" +
"4,8\n" +
"5,10\n" +
"6,12\n" +
"7,14\n" +
"8,16\n", {
// options go here. See http://dygraphs.com/options.html
legend: 'always',
animatedZooms: true,
plotter: barChartPlotter,
dateWindow: [0, 9]
});
功能柱状图绘图仪(e){
var myColor=[“ECD078”、“D95B43”、“C02942”、“542437”、“53777A”、“42d7f4”、“红色”、“绿色”];
变量myColor1=[“黑色”、“红色”、“绿色”、“ECD078”、“D95B43”、“C02942”、“542437”、“53777A”];
var ctx=e.drawingContext;
var点=e点;
变量y_bottom=e.dygraph.toDomYCoord(0);
//这实际上应该以最小间隙为基础
变量栏宽度=2/3*(点[1]。canvasx-点[0]。canvasx);
//浅色可能更美观
//进行实际绘图。
对于(变量i=0;i
以下是为同一条添加两种不同颜色的代码:
<div id="graph"></div>
function barChartPlotter(e) {
var myColor = ["#ECD078", "#D95B43", "#C02942", "#542437", "#53777A", "#42d7f4", "red", "green"];
var myColor1 = ["black", "red", "green", "#ECD078", "#D95B43", "#C02942", "#542437", "#53777A"];
var ctx = e.drawingContext;
var points = e.points;
var y_bottom = e.dygraph.toDomYCoord(0);
// This should really be based on the minimum gap
var bar_width = 2 / 3 * (points[1].canvasx - points[0].canvasx);
// a lighter shade might be more aesthetically pleasing
// Do the actual plotting.
for (var i = 0; i < points.length; i++) {
var p = points[i];
var center_x = p.canvasx; // center of the bar
ctx.fillStyle = myColor[i];
ctx.fillRect(center_x - bar_width / 2, p.canvasy, bar_width, y_bottom - p.canvasy);
ctx.strokeRect(center_x - bar_width / 2, p.canvasy, bar_width, y_bottom - p.canvasy);
}
for (var i = 0; i < points.length; i++) {
var p = points[i];
console.log(points[1]);
var center_x = p.canvasx; // center of the bar
ctx.fillStyle = myColor1[i];
ctx.fillRect(center_x - bar_width / 2, p.canvasy, bar_width, (y_bottom - p.canvasy) / 2);
ctx.strokeRect(center_x - bar_width / 2, p.canvasy, bar_width, (y_bottom - p.canvasy) / 2);
}
}
g = new Dygraph(document.getElementById("graph"),
"X,Y\n" +
"1,2\n" +
"2,4\n" +
"3,6\n" +
"4,8\n" +
"5,10\n" +
"6,12\n" +
"7,14\n" +
"8,16\n", {
// options go here. See http://dygraphs.com/options.html
legend: 'always',
animatedZooms: true,
plotter: barChartPlotter,
dateWindow: [0, 9]
});
功能柱状图绘图仪(e){
变量myColor=[“#ECD078”、“#D95B43”、“#C02942”