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.

basic_chart.md 2.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ## What is it
  2. A chart is generally a 2D rendition of data. For example, for a set of values across items, the data could look like:
  3. ```js
  4. data = {
  5. labels: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
  6. datasets: [
  7. { values: [18, 40, 30, 35, 8, 52, 17, -4] }
  8. ]
  9. }
  10. ```
  11. Rendering it doesn't require much more that that. Plug the data in with a [type]() `bar`, with an optional [color]() and [height]():
  12. ```js
  13. new frappe.Chart( "#chart", {
  14. data: data,
  15. type: 'bar',
  16. height: 140,
  17. colors: ['red']
  18. });
  19. ```
  20. <div class="demo" id="bar-basic-1"></div>
  21. And similarly, a `line` chart is data-wise homomorphic to a bar chart:
  22. ```js
  23. type:'line'
  24. ```
  25. <div class="demo" id="line-basic-1"></div>
  26. ## Adding more datasets
  27. A chart can have multiple datasets. In an axis chart, every dataset is represented individually.
  28. ```js
  29. data: {
  30. labels: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
  31. datasets: [
  32. { name: "Dataset 1", values: [18, 40, 30, 35, 8, 52, 17, -4] },
  33. { name: "Dataset 2", values: [30, 50, -10, 15, 18, 32, 27, 14] }
  34. ]
  35. }
  36. ```
  37. <div class="demo" id="multi-dataset-line-bar"></div>
  38. ## Responsiveness
  39. Frappe Charts are responsive, as they rerender all the data in the current available container width. To demonstrate, let's take the example of setting the [bar width]() for bar charts.
  40. In order to set the bar width, instead of defining it and the space between the bars independently, we simply define the <b>ratio of the space</b> between bars to the bar width. The chart then adjusts the actual size proportional to the chart container.
  41. ```js
  42. barOptions: {
  43. spaceRatio: 0.2 // default: 1
  44. },
  45. ```
  46. Try resizing the window to see the effect, with different ratio values.
  47. <div class="demo" id="bar-barwidth"></div>
  48. ## More Tweaks
  49. Axis lines define a chart presentation. By default they are long `span`ning lines, but to give prominence to data points, X and/or Y axes can also be short `tick`s:
  50. ```js
  51. axisOptions: {
  52. xAxisMode: 'tick' // default: 'span'
  53. },
  54. ```
  55. <div class="demo" id="bar-axis-tick"></div>
  56. Just like bar width, we can set the <b>dot size</b> on a line graph, with the [`dotSize`]() property in [`lineOptions`]().
  57. ```js
  58. lineOptions: {
  59. dotSize: 8 // default: 4
  60. },
  61. ```
  62. <div class="demo" id="line-dotsize"></div>
  63. These were some of the basic toggles to a chart; there are quite a few line options to go with, particularly to create [regions](). We'll look at those in next section.