|
| 1 | +--- |
| 2 | +title: Align Zero With Multiple Axes in Chart |
| 3 | +page_title: Align Zero With Multiple Axes in Chart | Kendo UI Chart for jQuery |
| 4 | +description: An example on how to align the zero value for multiple value axes in a Kendo UI Chart. |
| 5 | +type: how-to |
| 6 | +slug: chart-align-zero-multiple-axes |
| 7 | +tags: chart, align, zero, multiple, axes |
| 8 | +res_type: kb |
| 9 | +--- |
| 10 | + |
| 11 | +## Environment |
| 12 | + |
| 13 | +<table> |
| 14 | + <tr> |
| 15 | + <td>Product</td> |
| 16 | + <td>Chart for Progress® Kendo UI®</td> |
| 17 | + </tr> |
| 18 | +</table> |
| 19 | + |
| 20 | +## Description |
| 21 | + |
| 22 | +I have a chart with multiple axes. One has negavive values and the other does not. I want the zero on the two axes to be aligned and to show the negative values on one axis. |
| 23 | + |
| 24 | +## Solution |
| 25 | + |
| 26 | +1. Update the [min](https://docs.telerik.com/kendo-ui/api/javascript/dataviz/ui/chart/configuration/valueaxis.min) and [max](https://docs.telerik.com/kendo-ui/api/javascript/dataviz/ui/chart/configuration/valueaxis.max) setting for the value axes so they include both positive and negative values. |
| 27 | +1. Specify a function for the [valueAxis.labels.template](https://docs.telerik.com/kendo-ui/api/javascript/dataviz/ui/chart/configuration/valueaxis.labels#valueaxislabelstemplate) of the axes where only positive values should be displayed. |
| 28 | +1. Check the values in the template handler and return only the positive values. For the negative values return an empty string. |
| 29 | + |
| 30 | + |
| 31 | +```dojo |
| 32 | +<div id="chart"></div> |
| 33 | +</div> |
| 34 | +
|
| 35 | +<script> |
| 36 | + function onlyPositive(e) { |
| 37 | + if (e.value < 0) { |
| 38 | + return ''; |
| 39 | + } |
| 40 | +
|
| 41 | + return e.value; |
| 42 | + } |
| 43 | +
|
| 44 | +
|
| 45 | + $("#chart").kendoChart({ |
| 46 | + title: { |
| 47 | + text: "Hybrid car mileage report" |
| 48 | + }, |
| 49 | + legend: { |
| 50 | + position: "top" |
| 51 | + }, |
| 52 | + series: [{ |
| 53 | + type: "column", |
| 54 | + data: [20, 40, 45, 30, 50], |
| 55 | + stack: true, |
| 56 | + name: "on battery", |
| 57 | + color: "#cc6e38" |
| 58 | + }, { |
| 59 | + type: "column", |
| 60 | + data: [20, 30, 35, 35, 40], |
| 61 | + stack: true, |
| 62 | + name: "on gas", |
| 63 | + color: "#ef955f" |
| 64 | + }, { |
| 65 | + type: "line", |
| 66 | + data: [30, 38, 40, 32, 42], |
| 67 | + name: "mpg", |
| 68 | + color: "#ec5e0a", |
| 69 | + axis: "mpg" |
| 70 | + }, { |
| 71 | + type: "line", |
| 72 | + data: [-7.8, 6.2, 5.9, 7.4, 5.6], |
| 73 | + name: "l/100 km", |
| 74 | + color: "#4e4141", |
| 75 | + axis: "l100km" |
| 76 | + }], |
| 77 | + valueAxes: [{ |
| 78 | + title: { text: "miles" }, |
| 79 | + min: -100, |
| 80 | + max: 100, |
| 81 | + labels: { template: onlyPositive } |
| 82 | + }, { |
| 83 | + name: "km", |
| 84 | + title: { text: "km" }, |
| 85 | + min: -192, |
| 86 | + max: 192, |
| 87 | + majorUnit: 32, |
| 88 | + labels: { template: onlyPositive } |
| 89 | + }, { |
| 90 | + min: -60, |
| 91 | + max: 60, |
| 92 | + name: "mpg", |
| 93 | + title: { text: "miles per gallon" }, |
| 94 | + color: "#ec5e0a", |
| 95 | + labels: { template: onlyPositive } |
| 96 | + }, { |
| 97 | + name: "l100km", |
| 98 | + title: { text: "liters per 100km" }, |
| 99 | + color: "#4e4141" |
| 100 | + }], |
| 101 | + categoryAxis: { |
| 102 | + categories: ["Mon", "Tue", "Wed", "Thu", "Fri"], |
| 103 | + // Align the first two value axes to the left |
| 104 | + // and the last two to the right. |
| 105 | + // |
| 106 | + // Right alignment is done by specifying a |
| 107 | + // crossing value greater than or equal to |
| 108 | + // the number of categories. |
| 109 | + axisCrossingValues: [0, 0, 10, 10] |
| 110 | + } |
| 111 | + }); |
| 112 | +
|
| 113 | +</script> |
| 114 | +``` |
0 commit comments