polylinearTest.html 5.28 KB
Newer Older
afe's avatar
afe committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
<!DOCTYPE html>
<meta charset="utf-8">

<link href="../src/nv.d3.css" rel="stylesheet" type="text/css">
<link href="teststyle.css" rel="stylesheet" type='text/css'>
<style>

</style>
<body>
  <h3>Test cases for Domain and Range overrides - Example of a polylinear scale</h3>
  <div style='position:relative;'>
      <div class='navigation'>
          Tests:
          <a href="lineChartTest.html">Line Chart</a>
          <a href="stackedAreaChartTest.html">Stacked Area</a>
          <a href="../examples/cumulativeLineChart.html">Cumulative Line</a>
          <a href="ScatterChartTest.html">Scatter</a>
          <a href="pieChartTest.html">Pie chart</a>
      </div>
      <div class='chart full' id='chart1'>
        Line chart: yDomain = [0,2,200], yRange = [500,50,0]
        <svg></svg>
      </div>
      <div class='chart half with-transitions' id='chart2'>
        Historical bar chart: yDomain = [0,2,130], yRange = [500,50,0]
        <svg></svg>
      </div>

      <div class='half'>
        Notes:
        The chart.yRange() and chart.xRange() properties are an advanced feature.  They are useful
        in situations where your data has wild extremes: ie, you have lots of smaller numbers, and lots of really big numbers.<br/><br/>

        Without a polylinear scale, those really big data points will overwhelm the small points.<br/><br/>

        Please look at the examples to understand how polylinear scales work.  Comment/uncomment the lines that alter yDomain and yRange
        to see the effect it has on the charts.
      </div>
  </div>

<script src="../lib/d3.v3.js"></script>
<script src="../nv.d3.js"></script>
<script src="../src/tooltip.js"></script>
<script src="../src/utils.js"></script>
<script src="../src/interactiveLayer.js"></script>
<script src="../src/models/legend.js"></script>
<script src="../src/models/axis.js"></script>
<script src="../src/models/scatter.js"></script>
<script src="../src/models/line.js"></script>
<script src="../src/models/lineChart.js"></script>
<script src="../src/models/historicalBar.js"></script>
<script src="../src/models/historicalBarChart.js"></script>
<script>

//------------ CHART 1 
lineChartConfig("chart1", sinAndCos(),true, false);
barChartConfig(dataWithSpikes());


function lineChartConfig(containerid, data, guideline, useDates, auxOptions) {
  if (auxOptions === undefined) auxOptions = {};
  if (guideline === undefined) guideline = true;
  nv.addGraph(function() {
    var chart;
    chart = nv.models.lineChart().useInteractiveGuideline(guideline);

    var fullChartHeight = 500 - chart.margin().top - chart.margin().bottom;
    chart
        .x(function(d,i) { 
          return d.x;
        })
       .yDomain([0,2,200])   //Using a polylinear scale
        .yRange([fullChartHeight,50,0])
        ;

    if (auxOptions.width) 
      chart.width(auxOptions.width);

    if (auxOptions.height)
      chart.height(auxOptions.height);

    var formatter;
    if (useDates) {
        formatter = function(d,i) {
                var now = (new Date()).getTime() - 86400 * 1000 * 365;
                now = new Date(now + d * 86400 * 1000);
                return d3.time.format('%b %d %Y')(now );
        }
    }
    else {
        formatter = d3.format(",.1f");
    }

    chart.xAxis // chart sub-models (ie. xAxis, yAxis, etc) when accessed directly, return themselves, not the parent chart, so need to chain separately
        .tickFormat(
            formatter
          );

    chart.yAxis
        .axisLabel('Voltage (v)')
        .tickFormat(d3.format(',.2f'));

    d3.select('#' + containerid + ' svg')
        .datum(data)
      .transition().duration(500)
        .call(chart);

    nv.utils.windowResize(chart.update);

    return chart;
  });
}

function barChartConfig(data) {
    nv.addGraph(function() {
      var chart = nv.models.historicalBarChart();
      var fullChartHeight = 500 - chart.margin().top - chart.margin().bottom;
      chart.color(["#ff7f0e"])
      .yDomain([0,2,130])
      .yRange([fullChartHeight,50,0])
      .transitionDuration(500)
      ;

      chart.xAxis 
      .tickFormat(d3.format(',.1f'))
      .axisLabel("Time")
      ;

       chart.yAxis
      .axisLabel('Random Number')
      .tickFormat(d3.format(',.4f'));
      d3.select("#chart2 svg")
        .datum(data)
        .transition().duration(500)
        .call(chart);

      nv.utils.windowResize(chart.update);
      return chart;
    });
}

function sinAndCos() {
  var sin = [],
      cos = [],
      higherOrder = []
      ;

  var now = (new Date()).getTime();
  for (var i = 0; i < 100; i++) {
    sin.push({x: i, y:  Math.abs(Math.sin(i/10)) }); //the nulls are to show how defined works
    cos.push({x: i, y: Math.abs(.5 * Math.cos(i/10)) });

    higherOrder.push({x: i, y: Math.cos(i) * 100 + 100});
  }

  return [
    {
      values: sin,
      key: "Sine Wave",
      color: "#ff7f0e"
    },
    {
      values: cos,
      key: "Cosine Wave",
      color: "#2ca02c"
    },
    {
      values: higherOrder,
      key: "Higher Order",
      color: "#fc34ff"
    }
  ];
}

function dataWithSpikes() {
   var rval = [];
   for(var i =0; i < 100; i++) {

      var spike = (Math.random() > 0.9);
      rval.push({
        x: i,
        y: (spike) ? Math.random() * 100 + 30 : Math.random()
      });
   }
   return [
      {key: "Series 1",
      color : "orange",
      values: rval}
   ];
}

</script>