How to add a straight line y-axis over a dimple js scatterplot and assign colour to it

287 views Asked by At

I would like to draw a horizontal average line on a dimple js scatterplot. I've tried to follow the solution from here .But I can only get a point instead of a line and assignColor doesn't work either. I can't figure out what the problems are. Any suggestions? Your help would be appreciated.

      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

There are 2 answers

0
Ariet17 On

Based on the solution from here.

I changed my code to the following:

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

Then the problems are solved.

Note:

527.967 ≈ (600 - 500)/2 + 500 - (13.22 *500)/(50*6)

  • 600: svg height
  • 500: chart height
  • 13.22 (target value): Average grade, y-axis
  • 50: Interval between tick marks on the y-axis
  • 6: Total number of tick intervals on the y-axis
0
subramanian On

there is no direct way to add a average line in dimple charts , but we can add n number of series to it , so once u have added the required visualization bar,area,line etc . add another one like averageline series to ur code . this series should have only two data points , 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>