gpt4 book ai didi

javascript - dimple js直线y轴在条形图上

转载 作者:行者123 更新时间:2023-11-30 17:31:28 25 4
gpt4 key购买 nike

我正在尝试在 dimple js 条形图上绘制平均值、高值和低值直线。我不知道如何将它们绘制在 y 轴(成本)上作为穿过条形的直线。这是将高值、低值和平均值保存到需要在图表上绘制的相应变量中的 fiddle 。任何解决方案? jsfiddle 链接:http://jsfiddle.net/Ra2xS/14/

var dim = {"width":590,"height":450}; //chart container width
var data = [{"date":"01-02-2010","cost":"11.415679194952766"},{"date":"01-03-2010","cost":"10.81875691467018"},{"date":"01-04-2010","cost":"12.710197879070897"}];

//y- axis (cost) values to plot as straight line over bar chart in different colours
var avg = "11.65";
var high= "12.71";
var low = "10.82";

function barplot(id,dim,data)
{
keys = Object.keys(data[0]);
var xcord = keys[0];
var ycord = keys[1];
var svg = dimple.newSvg(id, dim.width, dim.height);

var myChart = new dimple.chart(svg,data);
myChart.setBounds(60, 30, 505, 305);

var x = myChart.addCategoryAxis("x", xcord);
x.addOrderRule(xcord);
x.showGridlines = true;

var y = myChart.addMeasureAxis("y", ycord);
y.showGridlines = true;
y.tickFormat = ',.1f';

var s = myChart.addSeries(null, dimple.plot.bar);
var s1 = myChart.addSeries(null, dimple.plot.line);
s1.lineWeight = 3;
s1.lineMarkers = true;

myChart.draw(1500);

}

barplot("body",dim,data);

最佳答案

你可以通过添加一个单独的系列来实现它自己的数据,不幸的是在版本 1.1.5 中有一个多线系列的错误,这意味着线标记变得困惑(所以我已经从下面的代码中删除它们)。好消息是我刚刚重写了所有的折线图代码,这个问题将在下一个版本中得到解决(一周左右),而且该线将通过从 x 轴上升而不是淡入而正确地动画化来自黑色。

var dim = {"width":590,"height":450}; //chart container width
var data = [{"date":"01-02-2010","cost":"11.415679194952766"},{"date":"01-03-2010","cost":"10.81875691467018"},{"date":"01-04-2010","cost":"12.710197879070897"}];


function barplot(id,dim,data)
{
keys = Object.keys(data[0]);
var xcord = keys[0];
var ycord = keys[1];
var svg = dimple.newSvg(id, dim.width, dim.height);
var parser = d3.time.format("%d-%m-%Y")
var dateReader = function (d) { return parser.parse(d[xcord]); }
var start = d3.time.month.offset(d3.min(data, dateReader), -1);
var end = d3.time.month.offset(d3.max(data, dateReader), 1);

var myChart = new dimple.chart(svg,data);
myChart.setBounds(60, 30, 505, 305);

//var x = myChart.addCategoryAxis("x", xcord);
var x = myChart.addTimeAxis("x", xcord, "%d-%m-%Y","%b %Y");
x.overrideMin = start;
x.overrideMax = end;
x.addOrderRule(xcord);
x.showGridlines = true;
x.timePeriod = d3.time.months;
x.floatingBarWidth = 100;

var y = myChart.addMeasureAxis("y", ycord);
y.showGridlines = true;
y.tickFormat = ',.1f';

var s1 = myChart.addSeries(null, dimple.plot.bar);
var s2 = myChart.addSeries(null, dimple.plot.line);
s2.lineWeight = 3;

var s3 = myChart.addSeries("Price Break", dimple.plot.line);
s3.data = [
{ "Price Break" : "high", "cost" : 12.71, "date" : parser(start) },
{ "Price Break" : "high", "cost" : 12.71, "date" : parser(end) },
{ "Price Break" : "avg", "cost" : 11.65, "date" : parser(start) },
{ "Price Break" : "avg", "cost" : 11.65, "date" : parser(end) },
{ "Price Break" : "low", "cost" : 10.82, "date" : parser(start) },
{ "Price Break" : "low", "cost" : 10.82, "date" : parser(end) }
];

myChart.draw(1500);

}

barplot("body",dim,data);

http://jsfiddle.net/Ra2xS/28/

关于javascript - dimple js直线y轴在条形图上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22989320/

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