You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

01-Line-Chart.md 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. ---
  2. title: Line Chart
  3. anchor: line-chart
  4. ---
  5. ###Introduction
  6. A line chart is a way of plotting data points on a line.
  7. Often, it is used to show trend data, and the comparison of two data sets.
  8. <div class="canvas-holder">
  9. <canvas width="250" height="125"></canvas>
  10. </div>
  11. ###Example usage
  12. ```javascript
  13. var myLineChart = new Chart(ctx).Line(data, options);
  14. ```
  15. ###Data structure
  16. ```javascript
  17. var data = {
  18. labels: ["January", "February", "March", "April", "May", "June", "July"],
  19. datasets: [
  20. {
  21. label: "My First dataset",
  22. fillColor: "rgba(220,220,220,0.2)",
  23. strokeColor: "rgba(220,220,220,1)",
  24. pointColor: "rgba(220,220,220,1)",
  25. pointStrokeColor: "#fff",
  26. pointHighlightFill: "#fff",
  27. pointHighlightStroke: "rgba(220,220,220,1)",
  28. data: [65, 59, 80, 81, 56, 55, 40]
  29. },
  30. {
  31. label: "My Second dataset",
  32. fillColor: "rgba(151,187,205,0.2)",
  33. strokeColor: "rgba(151,187,205,1)",
  34. pointColor: "rgba(151,187,205,1)",
  35. pointStrokeColor: "#fff",
  36. pointHighlightFill: "#fff",
  37. pointHighlightStroke: "rgba(151,187,205,1)",
  38. data: [28, 48, 40, 19, 86, 27, 90]
  39. }
  40. ]
  41. };
  42. ```
  43. The line chart requires an array of labels for each of the data points. This is shown on the X axis.
  44. The data for line charts is broken up into an array of datasets. Each dataset has a colour for the fill, a colour for the line and colours for the points and strokes of the points. These colours are strings just like CSS. You can use RGBA, RGB, HEX or HSL notation.
  45. The label key on each dataset is optional, and can be used when generating a scale for the chart.
  46. ### Chart options
  47. These are the customisation options specific to Line charts. These options are merged with the [global chart configuration options](#getting-started-global-chart-configuration), and form the options of the chart.
  48. ```javascript
  49. {
  50. ///Boolean - Whether grid lines are shown across the chart
  51. scaleShowGridLines : true,
  52. //String - Colour of the grid lines
  53. scaleGridLineColor : "rgba(0,0,0,.05)",
  54. //Number - Width of the grid lines
  55. scaleGridLineWidth : 1,
  56. //Boolean - Whether to show horizontal lines (except X axis)
  57. scaleShowHorizontalLines: true,
  58. //Boolean - Whether to show vertical lines (except Y axis)
  59. scaleShowVerticalLines: true,
  60. //Boolean - Whether the line is curved between points
  61. bezierCurve : true,
  62. //Number - Tension of the bezier curve between points
  63. bezierCurveTension : 0.4,
  64. //Boolean - Whether to show a dot for each point
  65. pointDot : true,
  66. //Number - Radius of each point dot in pixels
  67. pointDotRadius : 4,
  68. //Number - Pixel width of point dot stroke
  69. pointDotStrokeWidth : 1,
  70. //Number - amount extra to add to the radius to cater for hit detection outside the drawn point
  71. pointHitDetectionRadius : 20,
  72. //Boolean - Whether to show a stroke for datasets
  73. datasetStroke : true,
  74. //Number - Pixel width of dataset stroke
  75. datasetStrokeWidth : 2,
  76. //Boolean - Whether to fill the dataset with a colour
  77. datasetFill : true,
  78. {% raw %}
  79. //String - A legend template
  80. legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].strokeColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"
  81. {% endraw %}
  82. };
  83. ```
  84. You can override these for your `Chart` instance by passing a second argument into the `Line` method as an object with the keys you want to override.
  85. For example, we could have a line chart without bezier curves between points by doing the following:
  86. ```javascript
  87. new Chart(ctx).Line(data, {
  88. bezierCurve: false
  89. });
  90. // This will create a chart with all of the default options, merged from the global config,
  91. // and the Line chart defaults, but this particular instance will have `bezierCurve` set to false.
  92. ```
  93. We can also change these defaults values for each Line type that is created, this object is available at `Chart.defaults.Line`.
  94. ### Prototype methods
  95. #### .getPointsAtEvent( event )
  96. Calling `getPointsAtEvent(event)` on your Chart instance passing an argument of an event, or jQuery event, will return the point elements that are at that the same position of that event.
  97. ```javascript
  98. canvas.onclick = function(evt){
  99. var activePoints = myLineChart.getPointsAtEvent(evt);
  100. // => activePoints is an array of points on the canvas that are at the same position as the click event.
  101. };
  102. ```
  103. This functionality may be useful for implementing DOM based tooltips, or triggering custom behaviour in your application.
  104. #### .update( )
  105. Calling `update()` on your Chart instance will re-render the chart with any updated values, allowing you to edit the value of multiple existing points, then render those in one animated render loop.
  106. ```javascript
  107. myLineChart.datasets[0].points[2].value = 50;
  108. // Would update the first dataset's value of 'March' to be 50
  109. myLineChart.update();
  110. // Calling update now animates the position of March from 90 to 50.
  111. ```
  112. #### .addData( valuesArray, label )
  113. Calling `addData(valuesArray, label)` on your Chart instance passing an array of values for each dataset, along with a label for those points.
  114. ```javascript
  115. // The values array passed into addData should be one for each dataset in the chart
  116. myLineChart.addData([40, 60], "August");
  117. // This new data will now animate at the end of the chart.
  118. ```
  119. #### .removeData( )
  120. Calling `removeData()` on your Chart instance will remove the first value for all datasets on the chart.
  121. ```javascript
  122. myLineChart.removeData();
  123. // The chart will remove the first point and animate other points into place
  124. ```