[英]Amcharts, replace 0 with 1 in vertical valueAxis
尋找一種使下圖從1而不是0遞增的方法。
我可以在valueAxes
配置部分中設置minimum: 1
,但無論行出現在哪里,都將1強制為其顯示內容的下限。
我在amcharts文檔中看不到任何類似信息,因此我實際上並不認為有可能,但我經常錯了,所以我希望這樣。
沒有任何一站式解決方案可滿足您的需求。
結合使用minimum: 1
和(或不使用) strictMinMax
將強制比例尺從特定數字開始,而不管圖表變量的實際范圍如何。
您可以嘗試其他兩種“參與”的方法。
在構建圖表之前,請循環遍歷圖表數據,並僅在其值接近時設置minimum
。
/** * Create the chart */ var chart = AmCharts.makeChart("chartdiv", { "type": "serial", "theme": "light", "path": "http://www.amcharts.com/lib/3/", "dataProvider": [{ "year": "1950", "value": 9 }, { "year": "1951", "value": 30 }, { "year": "1952", "value": 35 }, { "year": "1953", "value": 25 }, { "year": "1954", "value": 70 }, { "year": "1955", "value": 45 }, { "year": "1956", "value": 55 }], "valueAxes": [{ "reversed": true, // these are the made up properties that are used by our custom code // set value axis minimum with "tentativeMinimum" if there are // values withing "minimumThreshold" to it "tentativeMinimum": 1, "minimumThreshold": 9 }], "graphs": [{ "id": "g1", "bullet": "round", "lineThickness": 2, "type": "smoothedLine", "valueField": "value" }], "categoryField": "year", "categoryAxis": { "labelsEnabled": false, } }); /** * Add chart pre-processor */ AmCharts.addInitHandler( function( chart ) { // check if there are any value axes defined if ( typeof chart.valueAxes !== "object" || ! chart.valueAxes.length ) return; // check if chart's value axis has "tentativeMinimum" set // For the sake of simplicity we're just gonna take the first // value axis and first graph. // Normally we would want to check all value axes and their attached // graphs to check for their respective values. var axis = chart.valueAxes[0]; if ( axis.tentativeMinimum === undefined || axis.minimumThreshold === undefined ) return; // get first charts valueField to check agains var field = chart.graphs[0].valueField; // cycle through the data var min; for ( var x = 0; x < chart.dataProvider.length; x++ ) { if ( min === undefined || ( chart.dataProvider[x][field] < min ) ) min = chart.dataProvider[x][field]; } // check if min is within the threshold if ( min <= ( axis.tentativeMinimum + axis.minimumThreshold ) ) axis.minimum = axis.tentativeMinimum; }, [ "serial" ] );
#chartdiv { width: 400px; height: 300px; }
<script src="http://www.amcharts.com/lib/3/amcharts.js"></script> <script src="http://www.amcharts.com/lib/3/serial.js"></script> <script src="http://www.amcharts.com/lib/3/themes/light.js"></script> <div id="chartdiv"></div>
使用值軸的labelFunction
可以簡單地用1替換零:
var chart = AmCharts.makeChart("chartdiv", {
...
"valueAxes": [{
"reversed": true,
"labelFunction": function( label ) {
return label === 0 ? 1 : label;
}
}],
...
});
請注意,這不會影響實際值軸刻度。 但是,差異可能並不明顯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.