選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

00-Getting-Started.md 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. ---
  2. title: Getting started
  3. anchor: getting-started
  4. ---
  5. ###Include Chart.js
  6. First we need to include the Chart.js library on the page. The library occupies a global variable of `Chart`.
  7. ```html
  8. <script src="Chart.js"></script>
  9. ```
  10. Alternatively, if you're using an AMD loader for JavaScript modules, that is also supported in the Chart.js core. Please note: the library will still occupy a global variable of `Chart`, even if it detects `define` and `define.amd`. If this is a problem, you can call `noConflict` to restore the global Chart variable to it's previous owner.
  11. ```javascript
  12. // Using requirejs
  13. require(['path/to/Chartjs'], function(Chart){
  14. // Use Chart.js as normal here.
  15. // Chart.noConflict restores the Chart global variable to it's previous owner
  16. // The function returns what was previously Chart, allowing you to reassign.
  17. var Chartjs = Chart.noConflict();
  18. });
  19. ```
  20. You can also grab Chart.js using bower:
  21. ```bash
  22. bower install Chart.js --save
  23. ```
  24. ###Creating a chart
  25. To create a chart, we need to instantiate the `Chart` class. To do this, we need to pass in the 2d context of where we want to draw the chart. Here's an example.
  26. ```html
  27. <canvas id="myChart" width="400" height="400"></canvas>
  28. ```
  29. ```javascript
  30. // Get the context of the canvas element we want to select
  31. var ctx = document.getElementById("myChart").getContext("2d");
  32. var myNewChart = new Chart(ctx).PolarArea(data);
  33. ```
  34. We can also get the context of our canvas with jQuery. To do this, we need to get the DOM node out of the jQuery collection, and call the `getContext("2d")` method on that.
  35. ```javascript
  36. // Get context with jQuery - using jQuery's .get() method.
  37. var ctx = $("#myChart").get(0).getContext("2d");
  38. // This will get the first returned node in the jQuery collection.
  39. var myNewChart = new Chart(ctx);
  40. ```
  41. After we've instantiated the Chart class on the canvas we want to draw on, Chart.js will handle the scaling for retina displays.
  42. With the Chart class set up, we can go on to create one of the charts Chart.js has available. In the example below, we would be drawing a Polar area chart.
  43. ```javascript
  44. new Chart(ctx).PolarArea(data, options);
  45. ```
  46. We call a method of the name of the chart we want to create. We pass in the data for that chart type, and the options for that chart as parameters. Chart.js will merge the global defaults with chart type specific defaults, then merge any options passed in as a second argument after data.
  47. ###Global chart configuration
  48. This concept was introduced in Chart.js 1.0 to keep configuration DRY, and allow for changing options globally across chart types, avoiding the need to specify options for each instance, or the default for a particular chart type.
  49. ```javascript
  50. Chart.defaults.global = {
  51. // Boolean - Whether to animate the chart
  52. animation: true,
  53. // Number - Number of animation steps
  54. animationSteps: 60,
  55. // String - Animation easing effect
  56. animationEasing: "easeOutQuart",
  57. // Boolean - If we should show the scale at all
  58. showScale: true,
  59. // Boolean - If we want to override with a hard coded scale
  60. scaleOverride: false,
  61. // ** Required if scaleOverride is true **
  62. // Number - The number of steps in a hard coded scale
  63. scaleSteps: null,
  64. // Number - The value jump in the hard coded scale
  65. scaleStepWidth: null,
  66. // Number - The scale starting value
  67. scaleStartValue: null,
  68. // String - Colour of the scale line
  69. scaleLineColor: "rgba(0,0,0,.1)",
  70. // Number - Pixel width of the scale line
  71. scaleLineWidth: 1,
  72. // Boolean - Whether to show labels on the scale
  73. scaleShowLabels: true,
  74. // Interpolated JS string - can access value
  75. scaleLabel: "<%=value%>",
  76. // Boolean - Whether the scale should stick to integers, not floats even if drawing space is there
  77. scaleIntegersOnly: true,
  78. // Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
  79. scaleBeginAtZero: false,
  80. // String - Scale label font declaration for the scale label
  81. scaleFontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
  82. // Number - Scale label font size in pixels
  83. scaleFontSize: 12,
  84. // String - Scale label font weight style
  85. scaleFontStyle: "normal",
  86. // String - Scale label font colour
  87. scaleFontColor: "#666",
  88. // Boolean - whether or not the chart should be responsive and resize when the browser does.
  89. responsive: false,
  90. // Boolean - whether to maintain the starting aspect ratio or not when responsive, if set to false, will take up entire container
  91. maintainAspectRatio: true,
  92. // Boolean - Determines whether to draw tooltips on the canvas or not
  93. showTooltips: true,
  94. // Function - Determines whether to execute the customTooltips function instead of drawing the built in tooltips (See [Advanced - External Tooltips](#advanced-usage-custom-tooltips))
  95. customTooltips: false,
  96. // Array - Array of string names to attach tooltip events
  97. tooltipEvents: ["mousemove", "touchstart", "touchmove"],
  98. // String - Tooltip background colour
  99. tooltipFillColor: "rgba(0,0,0,0.8)",
  100. // String - Tooltip label font declaration for the scale label
  101. tooltipFontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
  102. // Number - Tooltip label font size in pixels
  103. tooltipFontSize: 14,
  104. // String - Tooltip font weight style
  105. tooltipFontStyle: "normal",
  106. // String - Tooltip label font colour
  107. tooltipFontColor: "#fff",
  108. // String - Tooltip title font declaration for the scale label
  109. tooltipTitleFontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
  110. // Number - Tooltip title font size in pixels
  111. tooltipTitleFontSize: 14,
  112. // String - Tooltip title font weight style
  113. tooltipTitleFontStyle: "bold",
  114. // String - Tooltip title font colour
  115. tooltipTitleFontColor: "#fff",
  116. // Number - pixel width of padding around tooltip text
  117. tooltipYPadding: 6,
  118. // Number - pixel width of padding around tooltip text
  119. tooltipXPadding: 6,
  120. // Number - Size of the caret on the tooltip
  121. tooltipCaretSize: 8,
  122. // Number - Pixel radius of the tooltip border
  123. tooltipCornerRadius: 6,
  124. // Number - Pixel offset from point x to tooltip edge
  125. tooltipXOffset: 10,
  126. {% raw %}
  127. // String - Template string for single tooltips
  128. tooltipTemplate: "<%if (label){%><%=label%>: <%}%><%= value %>",
  129. {% endraw %}
  130. // String - Template string for multiple tooltips
  131. multiTooltipTemplate: "<%= value %>",
  132. // Function - Will fire on animation progression.
  133. onAnimationProgress: function(){},
  134. // Function - Will fire on animation completion.
  135. onAnimationComplete: function(){}
  136. }
  137. ```
  138. If for example, you wanted all charts created to be responsive, and resize when the browser window does, the following setting can be changed:
  139. ```javascript
  140. Chart.defaults.global.responsive = true;
  141. ```
  142. Now, every time we create a chart, `options.responsive` will be `true`.