gpt4 book ai didi

javascript - dygraph - 如何更改条形图的系列名称

转载 作者:行者123 更新时间:2023-12-03 12:36:02 25 4
gpt4 key购买 nike

我有一个包含 3 个系列的条形图,但我无法更改它们的名称,我只有 y1 y2 y2,而不是代码中我自己的名称。例如bar chart我明白了,我应该使用 Multi-columnBarChart,但我该怎么做呢?这是来自示例 Multi-columnBarChart 的评论:

function multiColumnBarPlotter(e) {
// We need to handle all the series simultaneously.
if (e.seriesIndex !== 0) return;

var g = e.dygraph;
var ctx = e.drawingContext;
var sets = e.allSeriesPoints;
var y_bottom = e.dygraph.toDomYCoord(0);

// Find the minimum separation between x-values.
// This determines the bar width.
var min_sep = Infinity;
for (var j = 0; j < sets.length; j++) {
var points = sets[j];
for (var i = 1; i < points.length; i++) {
var sep = points[i].canvasx - points[i - 1].canvasx;
if (sep < min_sep) min_sep = sep;
}
}
var bar_width = Math.floor(2.0 / 3 * min_sep);

这是我的代码:

function barChartPlotter(e) {
var ctx = e.drawingContext;
var points = e.points;
var y_bottom = e.dygraph.toDomYCoord(0);

// The RGBColorParser class is provided by rgbcolor.js, which is
// packed in with dygraphs.
var color = new RGBColorParser(e.color);
color.r = Math.floor((255 + color.r) / 2);
color.g = Math.floor((255 + color.g) / 2);
color.b = Math.floor((255 + color.b) / 2);
ctx.fillStyle = color.toRGB();

// Find the minimum separation between x-values.
// This determines the bar width.
var min_sep = Infinity;
for (var i = 1; i < points.length; i++) {
var sep = points[i].canvasx - points[i - 1].canvasx;
if (sep < min_sep) min_sep = sep;
}
var bar_width = Math.floor(2.0 / 3 * min_sep);

// Do the actual plotting.
for (var i = 0; i < points.length; i++) {
var p = points[i];
var center_x = p.canvasx;

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);
}
}

data = [];
var x = js_array.length;
// CONVERTATION
pass = [];
fail = [];
tree = [];
for (var i=0; i<x; i++){
if (js_array[i] == 1) {
pass.push(js_array[i]);
fail.push(NaN);
tree.push(NaN);
}
else if (js_array[i] == 2) {
pass.push(NaN);
fail.push(js_array[i]);
tree.push(NaN);
}

else if (js_array[i] == 3) {
pass.push(NaN);
fail.push(NaN);
tree.push(js_array[i]);
}
}

// DATA
for (var i=0; i<x; i++){
var y = x - i - 1;
data.push([new Date(js_array_time[y]), pass[y], fail[y], tree[y]]);
}

seriesName = ['x','PASS', 'FAIL', 'TREE']
// OPTIONS
var start = new Date(js_array_time[9]);
var stop = new Date(js_array_time[0]);
start.setMinutes(start.getMinutes() - 10);
stop.setMinutes(stop.getMinutes() + 10);

s = document.getElementById("s");

g = new Dygraph(
document.getElementById("graphdiv"),
data,
{
width: 580,
includeZero: true,
animatedZooms: true,
plotter: barChartPlotter,
axisLineWidth: 0.001,
//labels: seriesName,
legend: 'always',
title: 'Test results',
drawYAxis:false,
valueRange: [0.5, 3.5],
dateWindow: [Date.parse(start), Date.parse(stop)],
pointClickCallback: function(e, p) {
//s.innerHTML += "<b>Point Click</b> " + p.name + ": " + p.xval + "<br/>";
window.open('../RUNNER/_VLOGS/launch' + p.xval + '.html','_blank');
window.open('../RUNNER/_LOGS/launch_log' + p.xval + '.txt','_blank');
},
//drawPoints: true,
//drawXGrid: false,
//drawYGrid: false,
//fillGraph: true

}
);

g.ready(function() {
g.setAnnotations([
{

}]);
});

最佳答案

对于多个系列,您需要获取allSeriesPoints,而不仅仅是points。这是我为另一个 SO 问题整理的示例:

Here is a working DEMO at jsFiddle.com

绘图仪代码如下所示:

function multiColumnBarPlotter(e) {
// We need to handle all the series simultaneously.
if (e.seriesIndex !== 0) return;

var g = e.dygraph;
var ctx = e.drawingContext;
var sets = e.allSeriesPoints;
var y_bottom = e.dygraph.toDomYCoord(0);

// Find the minimum separation between x-values.
// This determines the bar width.
var min_sep = Infinity;
for (var j = 0; j < sets.length; j++) {
var points = sets[j];
for (var i = 1; i < points.length; i++) {
var sep = points[i].canvasx - points[i - 1].canvasx;
if (sep < min_sep) min_sep = sep;
}
}
var bar_width = Math.floor(2.0 / 3 * min_sep);

var fillColors = [];
var strokeColors = g.getColors();
for (var i = 0; i < strokeColors.length; i++) {
var color = new RGBColorParser(strokeColors[i]);
color.r = Math.floor((255 + color.r) / 2);
color.g = Math.floor((255 + color.g) / 2);
color.b = Math.floor((255 + color.b) / 2);
fillColors.push(color.toRGB());
}

for (var j = 0; j < sets.length; j++) {
ctx.fillStyle = fillColors[j];
ctx.strokeStyle = strokeColors[j];
for (var i = 0; i < sets[j].length; i++) {
var p = sets[j][i];
var center_x = p.canvasx;
var x_left = center_x - (bar_width / 1.3) * (1 - j/(sets.length-1));

ctx.fillRect(x_left, p.canvasy,
bar_width/sets.length, y_bottom - p.canvasy);

ctx.strokeRect(x_left, p.canvasy,
bar_width/sets.length, y_bottom - p.canvasy);
}
}
}

然后,我创建 2 个相同数据的图表,一个使用 CSV,另一个使用数组。 CSV 直接在数据中提供标签,对于数组,您可以使用 labels 属性更改标签:

labels: ['x', 'series1', 'series2', 'series3'],

整个图表代码将是

g2 = new Dygraph(document.getElementById("g_div2"),
theData,
{
// options go here. See http://dygraphs.com/options.html
legend: 'always',
labels: ['x', 'series1', 'series2', 'series3'],
animatedZooms: true,
plotter: multiColumnBarPlotter,
colors: ["#00A0B0", "#6A4A3C", "#CC333F", ],
dateWindow: [0, 8]
});

关于javascript - dygraph - 如何更改条形图的系列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23733548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com