|
| 1 | +--- |
| 2 | +title: Displaying a Year's Data in a Chart Zoomed to the Last 30 Days |
| 3 | +description: Learn how to present a chart with 365 days of data initially zoomed to show only the last 30 days using Kendo UI Chart. |
| 4 | +type: how-to |
| 5 | +page_title: How to Zoom into the Last 30 Days on a Kendo UI Chart |
| 6 | +slug: how-to-display-chart-data-zoomed-to-last-30-days |
| 7 | +tags: kendo ui, chart, zoom, categoryaxis, date, data visualization |
| 8 | +res_type: kb |
| 9 | +ticketid: 1627452 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td> |
| 19 | +Chart for Progress® Kendo UI®, <br /> |
| 20 | +</td> |
| 21 | +</tr> |
| 22 | +<tr> |
| 23 | +<td>Version</td> |
| 24 | +<td>2024.3.1015</td> |
| 25 | +</tr> |
| 26 | +</tbody> |
| 27 | +</table> |
| 28 | + |
| 29 | +## Description |
| 30 | + |
| 31 | +I need to display a chart with 365 days of data but want it zoomed by default to only show the last 30 days. This KB article also answers the following questions: |
| 32 | +- How to set the initial zoom level of a Kendo UI Chart? |
| 33 | +- How to display a specific date range in a Kendo UI Chart by default? |
| 34 | +- How to configure the category axis of a Kendo UI Chart for specific date ranges? |
| 35 | + |
| 36 | +## Solution |
| 37 | + |
| 38 | +To display a chart with 365 days of data initially zoomed to the last 30 days, configure the [`categoryAxis.min`](https://docs.telerik.com/kendo-ui/api/javascript/dataviz/ui/chart/configuration/categoryaxis.min) and [`categoryAxis.max`](https://docs.telerik.com/kendo-ui/api/javascript/dataviz/ui/chart/configuration/categoryaxis.max) options. These options control the initial zoom level by setting the visible date range on the chart. Here's how to set it up: |
| 39 | + |
| 40 | +1. Define the `categoryAxis` with a [`type`](https://docs.telerik.com/kendo-ui/api/javascript/dataviz/ui/chart/configuration/categoryaxis.type) of `"date"`. |
| 41 | +2. Set the `min` and `max` properties to define the visible date range. For example, to zoom in on December 1st to 31st, 2023: |
| 42 | + |
| 43 | +```javascript |
| 44 | +categoryAxis: { |
| 45 | + type: "date", |
| 46 | + min: new Date(2023, 11, 1), // December 1, 2023 |
| 47 | + max: new Date(2023, 11, 31) // December 31, 2023 |
| 48 | +}, |
| 49 | +``` |
| 50 | + |
| 51 | +This configuration zooms the chart to display only the specified date range, while still allowing users to zoom in and out within the full 365 days of data. |
| 52 | + |
| 53 | +If you need a more interactive approach, consider using the [Stock Chart](https://docs.telerik.com/kendo-ui/api/javascript/dataviz/ui/stockchart) with the Navigator enabled. This allows users to see that the chart displays a specific period and provides a visual way to adjust the zoom level. |
| 54 | + |
| 55 | +For an example of how to configure the `categoryAxis.min` and `max`, refer to next Dojo demo. |
| 56 | + |
| 57 | +```dojo |
| 58 | +<div id="example"> |
| 59 | +<div class="box wide"> |
| 60 | +<p>Use SHIFT + Mouse Drag Region Selection combination on mouse-enabled devices to zoom in data for a specific period of time</p> |
| 61 | +
|
| 62 | +</div> |
| 63 | + <div class="demo-section k-content wide"> |
| 64 | + <div id="chart"></div> |
| 65 | + </div> |
| 66 | + <script> |
| 67 | + // Sample data |
| 68 | + var data = []; |
| 69 | + for (var i = 0; i <= 365; i++) { |
| 70 | + var val = Math.round(Math.random() * 10); |
| 71 | + var firstDate = new Date(2023, 0, 1) |
| 72 | + data.push({ |
| 73 | + category: firstDate.setDate(firstDate.getDate() + i), |
| 74 | + value: val |
| 75 | + }); |
| 76 | + } |
| 77 | +
|
| 78 | + function createChart() { |
| 79 | + $("#chart").kendoChart({ |
| 80 | + dataSource: { |
| 81 | + data: data |
| 82 | + }, |
| 83 | + categoryAxis: { |
| 84 | + type: "date", |
| 85 | + min: new Date(2023, 11, 1), |
| 86 | + max: new Date(2023, 11, 32), |
| 87 | + labels: { |
| 88 | + rotation: "auto" |
| 89 | + } |
| 90 | + }, |
| 91 | + series: [{ |
| 92 | + type: "column", |
| 93 | + field: "value", |
| 94 | + categoryField: "category" |
| 95 | + }], |
| 96 | + pannable: { |
| 97 | + lock: "y" |
| 98 | + }, |
| 99 | + zoomable: { |
| 100 | + mousewheel: { |
| 101 | + lock: "y" |
| 102 | + }, |
| 103 | + selection: { |
| 104 | + lock: "y" |
| 105 | + } |
| 106 | + } |
| 107 | + }); |
| 108 | + } |
| 109 | +
|
| 110 | + $(document).ready(createChart); |
| 111 | + $("#example").bind("kendo:skinChange", createChart); |
| 112 | + </script> |
| 113 | +</div> |
| 114 | +``` |
| 115 | + |
| 116 | +For an interactive example using the [`Stock Chart`](https://docs.telerik.com/kendo-ui/controls/charts/stockchart/overview) and Navigator, see the following Dojo demo. |
| 117 | + |
| 118 | +```dojo |
| 119 | + <div id="example"> |
| 120 | + <div class="demo-section k-content wide"> |
| 121 | + <div id="stock-chart"></div> |
| 122 | + </div> |
| 123 | + <script> |
| 124 | + function createChart() { |
| 125 | + $("#stock-chart").kendoStockChart({ |
| 126 | + dataSource: { |
| 127 | + transport: { |
| 128 | + read: { |
| 129 | + url: "../content/dataviz/js/boeing-stock.json", |
| 130 | + dataType: "json" |
| 131 | + } |
| 132 | + }, |
| 133 | + schema: { |
| 134 | + model: { |
| 135 | + fields: { |
| 136 | + Date: { type: "date" } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + }, |
| 141 | + title: { |
| 142 | + text: "The Boeing Company\nNYSE:BA" |
| 143 | + }, |
| 144 | + dateField: "Date", |
| 145 | + panes: [{ |
| 146 | + name: "volumePane", |
| 147 | + title: "Volume", |
| 148 | + height: 150 // pixels |
| 149 | + }], |
| 150 | + categoryAxis: { |
| 151 | + baseUnit: "days", |
| 152 | + pane: "volumePane", |
| 153 | + labels: { |
| 154 | + rotation: { |
| 155 | + angle: 45 |
| 156 | + }, |
| 157 | + step: 7 |
| 158 | + } |
| 159 | + }, |
| 160 | + valueAxes: [{ |
| 161 | + // Default axis |
| 162 | + line: { |
| 163 | + visible: false |
| 164 | + } |
| 165 | + }, { |
| 166 | + name: "volumeAxis", |
| 167 | + pane: "volumePane", |
| 168 | + visible: false |
| 169 | + }], |
| 170 | + series: [{ |
| 171 | + type: "column", |
| 172 | + field: "Volume", |
| 173 | + axis: "volumeAxis", |
| 174 | + tooltip: { |
| 175 | + format: "{0:C0}" |
| 176 | + } |
| 177 | + }], |
| 178 | + navigator: { |
| 179 | + series: { |
| 180 | + type: "area", |
| 181 | + field: "Close" |
| 182 | + }, |
| 183 | + select: { |
| 184 | + from: "2011/12/01", |
| 185 | + to: "2011/12/31" |
| 186 | + }, |
| 187 | + categoryAxis: { |
| 188 | + max: "2011/12/31", |
| 189 | + min: "2011/01/01" |
| 190 | + } |
| 191 | + } |
| 192 | + }); |
| 193 | + } |
| 194 | + $(document).ready(createChart); |
| 195 | + $(document).bind("kendo:skinChange", createChart); |
| 196 | + </script> |
| 197 | + </div> |
| 198 | +``` |
| 199 | + |
| 200 | +## See Also |
| 201 | + |
| 202 | +- [Chart Configuration - API ](https://docs.telerik.com/kendo-ui/api/javascript/dataviz/ui/chart/configuration.) |
| 203 | +- [Overview of the Chart Component](https://docs.telerik.com/kendo-ui/controls/charts/chart/overview) |
| 204 | +- [Overview of the Stock Chart Component](https://docs.telerik.com/kendo-ui/controls/charts/stockchart/overview) |
0 commit comments