我想在凹坑js散点图上画一条水平平均线。我试图按照here的解决方案 但是我只能得到一个点而不是一条线而且assignColor也不起作用。我无法弄清问题是什么。有什么建议?感谢您的帮助。
var svg = dimple.newSvg("#ChartContainer", 800, 600);
var myChart = new dimple.chart(svg, data);
myChart.setBounds(60, 50, 600, 500)
myChart.addMeasureAxis("x", "ESCIndex");
myChart.addMeasureAxis("y", "Grade");
var s=myChart.addSeries(["OECD"], dimple.plot.bubble);
var myLegend=myChart.addLegend(10, 10, 400, 30, "right", s);
var s1 = myChart.addSeries("Average", dimple.plot.line);
s1.data = [
{ "Average" : "avg", "Grade" : 13.22, "ESCIndex" : -10.45 },
{ "Average" : "avg", "Grade" : 13.22, "ESCIndex" : 6.79 }
];
myChart.assignColor("Average", "green");
myChart.draw();
答案 0 :(得分:0)
基于here.
的解决方案我将代码更改为以下内容:
var svg = dimple.newSvg("#ChartContainer", 800, 600);
var myChart = new dimple.chart(svg, data);
myChart.setBounds(60, 50, 600, 500)
myChart.addMeasureAxis("x", "ESCIndex");
myChart.addMeasureAxis("y", "Grade");
myChart.addSeries(["OECD"], dimple.plot.bubble);
var myLegend=myChart.addLegend(10, 10, 400, 30, "right");
myChart.draw();
svg.append("line")
.attr("x1", myChart._xPixels())
.attr("x2", myChart._xPixels() + myChart._widthPixels())
.attr("y1", 527.967)
.attr("y2", 527.967)
.style("stroke", "green");
然后问题就解决了。
527.967≈(600 - 500)/ 2 + 500 - (13.22 * 500)/(50 * 6)
答案 1 :(得分:0)
没有直接方法在酒窝图表中添加平均线,但是我们可以向其添加n个序列,因此一旦您添加了所需的可视化栏,区域,线等。在我们的代码中添加另一个类似averageline系列的代码。该系列应该只有两个数据点,datapoint1 = {key:YourData [1] .key,value:averageValue},datapoint2 = {key:YourData [last] .key,value:averageValue}
var svg = dimple.newSvg("#chartContainer", 600, 400),
data = [
{ "Value" : 100, "Year" : 2009 , "sample": "0"},
{ "Value" : 40, "Year" : 2010 , "sample": "0"},
{ "Value" : 60, "Year" : 2011 , "sample": "0"},
{ "Value" : 10, "Year" : 2012 , "sample": "0"},
{ "Value" : 80, "Year" : 2013 , "sample": "0"},
{ "Value" : 20, "Year" : 2014 , "sample": "0"}
];
// Draw a standard chart using the aggregated data
var chart = new dimple.chart(svg, data);
var x = chart.addCategoryAxis("x", "Year");
var y = chart.addMeasureAxis("y", "Value");
y.hidden = true;
var s = chart.addSeries('channel', dimple.plot.bar);
let horizontalLine = chart.addSeries("Average", dimple.plot.line);
chart.assignColor("Average","black");
horizontalLine.data = [];
for (let i = 0; i < 2; i++) {
let obj = {};
obj["Year"] = i ? data[0]["Year"] : data[data.length-1]["Year"];
obj["Value"] =50;
horizontalLine.data.push(obj);
}
chart.draw();
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.8/d3.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dimple/2.2.0/dimple.latest.js"></script>
<div id="chartContainer"></div>