如何在凹坑js散点图上添加直线y轴并为其指定颜色

时间:2017-12-20 13:43:21

标签: dimple.js horizontal-line

我想在凹坑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();

2 个答案:

答案 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)

  • 600:svg height
  • 500:图表高度
  • 13.22(目标值):平均等级,y轴
  • 50:y轴上的刻度线之间的间隔
  • 6:y轴上的滴答间隔总数

答案 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>