如何使用amchart.js隐藏图表中的零值?
这是我生成图表的代码:
function getStatsByFlowByOperation(){
$.ajax({
success: function(data) {
//data = formatData(data);
am4core.useTheme(am4themes_material);
chart = am4core.create("chartdiv", am4charts.XYChart);
chart.hiddenState.properties.opacity = 0;
chart.data = data;
chart.colors.step = 2;
chart.padding(30, 30, 10, 30);
chart.legend = new am4charts.Legend();
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "OPERATION_NAME";
categoryAxis.renderer.minGridDistance = 30;
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.labels.template.rotation = 315;
categoryAxis.renderer.labels.template.verticalCenter = "middle";
categoryAxis.renderer.labels.template.horizontalCenter = "right";
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.renderer.inside = true;
valueAxis.renderer.labels.template.disabled = true;
valueAxis.min = 0;
var allStatus = [];
allStatus.push("NEW");
allStatus.push("DISABLED");
allStatus.push("SKIPPED");
allStatus.push("READY");
for(var i = 0; i < allStatus.length; i++) {
createSeries(allStatus[i], allStatus[i]);
}
},
error: function (jqXHR, exception) {
if (globalVars.unloaded) {
return;
}
manageAjaxError(jqXHR, textStatus, errorThrown);
}
});
}
这是输入中日期的示例:
[{
"READY": 0,
" ": 1,
"OPERATION_NAME": "INIT",
"STEP": 1
}]
编辑-
解决方案是插入“ null”。即使列显示“ 0”值(例如:
if(data[i].READY == 0) {
data[i].READY = null;
}
答案 0 :(得分:1)
或者有一个简短的丑陋方式:
var numbers = document.getElementsByTagName('tspan');
for(var i = 0; i < numbers.length; i++){
if(numbers[i].innerHTML == '0'){
numbers[i].parentElement.removeChild(numbers[i]);
}
}
答案 1 :(得分:1)
要隐藏零值项目符号,应使用适配器(Amcharts 4)。无需调整数据或执行难看的JS技巧。 Amcharts实现了适配器来进行各种动态渲染修改。
对于柱形图,它看起来像:
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueY = field;
series.dataFields.categoryX = "category";
series.name = name;
series.columns.template.tooltipText = "{name}: {valueY}";
var bullet = series.bullets.push(new am4charts.LabelBullet);
bullet.label.text = "{name}: {valueY}";
bullet.label.fill = am4core.color("#ffffff");
bullet.locationY = 0.5;
bullet.label.adapter.add("textOutput", function(text, target) {
// Hide labels with 0 value
if(target.dataItem && target.dataItem.valueY == 0) {
return "";
}
return text;
});
答案 2 :(得分:0)
调整为将零更改为null,而不是在弄清问题后将其删除。
编辑测试2019-05-07
var objData = [
{ READY: 0, " ": 1, OPERATION_NAME: "INIT", STEP: 1 },
{ READY: 0, " ": 1, OPERATION_NAME: "EXPORT_SPEC", STEP: 2 },
{ READY: 0, " ": 1, OPERATION_NAME: "TRANSFORM_APS", STEP: 3 },
{ READY: 1, OPERATION_NAME: "PUBLISH_APS", STEP: 4 }
];
// change zero values to null
_.transform(objData, function(myObj, val, idx) {
if(val.READY==0){
val.READY = null;
}
console.log("val: "+JSON.stringify(val));
});
console.log("After Processing");
console.log(JSON.stringify(objData));
//am4core.useTheme(am4themes_material);
chart = am4core.create("chartdiv", am4charts.XYChart);
chart.hiddenState.properties.opacity = 0;
chart.data = objData;
chart.colors.step = 2;
chart.padding(30, 30, 10, 30);
chart.legend = new am4charts.Legend();
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "OPERATION_NAME";
categoryAxis.renderer.minGridDistance = 30;
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.labels.template.rotation = 315;
categoryAxis.renderer.labels.template.verticalCenter = "middle";
categoryAxis.renderer.labels.template.horizontalCenter = "right";
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.renderer.inside = true;
valueAxis.renderer.labels.template.disabled = true;
valueAxis.min = 0;
chart.colors.list = [
am4core.color("#8BC9FF"), // NEW
am4core.color("#FF4D4D"), // ERROR
am4core.color("#F2F2F2"), // DISABLED
am4core.color("#B5E8B5"), // FINISHED
am4core.color("#715AFF"), // SKIPPED
am4core.color("#FFEBCC"), // TIMEOUT
am4core.color("#BEECFF"), // READY
am4core.color("#EDD9BA"), // RESUBMITTED
am4core.color("#cceeff"), // DISPATCHED
am4core.color("#FFF4BE"), // CANCELLED
am4core.color("#FFFFB3"), // RUNNING
am4core.color("#CCFFCC"), // DONE
am4core.color("#FF4D4D") // ERROR_RESUMED
];
var allStatus = [];
allStatus.push("NEW");
allStatus.push("DISABLED");
allStatus.push("SKIPPED");
allStatus.push("READY");
allStatus.push("DISPATCHED");
allStatus.push("RUNNING");
allStatus.push("ERROR_RESUMED");
allStatus.push("ERROR");
allStatus.push("FINISHED");
allStatus.push("TIMEOUT");
allStatus.push("RESUBMITTED");
allStatus.push("CANCELLED");
allStatus.push("DONE");
for (var i = 0; i < allStatus.length; i++) {
createSeries(allStatus[i], allStatus[i]);
}
function isZero(x) { return x == 0; }
// Create series
function createSeries(field, name) {
// Set up series
var series = chart.series.push(new am4charts.ColumnSeries());
series.name = name;
series.dataFields.valueY = field;
series.dataFields.categoryX = "OPERATION_NAME";
series.sequencedInterpolation = true;
// Make it stacked
series.stacked = true;
// Configure columns
series.columns.template.width = am4core.percent(60);
series.columns.template.tooltipText =
"[bold]{name}[/]\n[font-size:14px]{categoryX}: {valueY}";
// Add label
var labelBullet = series.bullets.push(new am4charts.LabelBullet());
labelBullet.label.text = "{valueY}";
labelBullet.locationY = 0.5;
return series;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
height:97vh;
}
#chartdiv {
width: 100%;
height: 100%;
}
<script src="//www.amcharts.com/lib/4/core.js"></script>
<script src="//www.amcharts.com/lib/4/charts.js"></script>
<script src="//www.amcharts.com/lib/4/themes/animated.js">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<div id="chartdiv"></div>
答案 3 :(得分:-1)
amCharts中可能有一个参数或选项,但是我对使用该库没有经验,所以想到的第一个选择是从数据中删除该元素。
我知道最简单的方法就是用lodash。
您没有提供数据,所以我使用的是我可以用来说明的其他数据。
/**
* ---------------------------------------
* This demo was created using amCharts 4.
*
* For more information visit:
* https://www.amcharts.com/
*
* Documentation is available at:
* https://www.amcharts.com/docs/v4/
* ---------------------------------------
*/
am4core.useTheme(am4themes_animated);
var chart = am4core.create("chartdiv", am4charts.XYChart);
chart.hiddenState.properties.opacity = 0; // this creates initial fade-in
chart.colors.saturation = 0.4;
var objData = [{
"country": "USA",
"visits": 3025
}, {
"country": "China",
"visits": 1882
}, {
"country": "Japan",
"visits": 0
}, {
"country": "Germany",
"visits": 1322
}, {
"country": "UK",
"visits": 1122
}, {
"country": "France",
"visits": 1114
}, {
"country": "India",
"visits": 984
}, {
"country": "Spain",
"visits": 711
}, {
"country": "Netherlands",
"visits": 665
}, {
"country": "Russia",
"visits": 580
}, {
"country": "South Korea",
"visits": 443
}, {
"country": "Canada",
"visits": 441
}];
objData.visits = _.reject(objData.visits, (o) => {
return o.visits > 0;
});
_.remove(objData, function(currentObject) {
return currentObject.visits == 0;
});
console.log(JSON.stringify(objData));
chart.data = objData;
var categoryAxis = chart.yAxes.push(new am4charts.CategoryAxis());
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.dataFields.category = "country";
categoryAxis.renderer.minGridDistance = 20;
categoryAxis.renderer.minWidth = 120;
var valueAxis = chart.xAxes.push(new am4charts.ValueAxis());
valueAxis.renderer.maxLabelPosition = 0.98;
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.categoryY = "country";
series.dataFields.valueX = "visits";
series.tooltipText = "{valueX.value}";
series.sequencedInterpolation = true;
series.defaultState.transitionDuration = 1000;
series.sequencedInterpolationDelay = 100;
series.columns.template.strokeOpacity = 0;
chart.cursor = new am4charts.XYCursor();
chart.cursor.behavior = "zoomY";
// as by default columns of the same series are of the same color, we add adapter which takes colors from chart.colors color set
series.columns.template.adapter.add("fill", (fill, target) => {
return chart.colors.getIndex(target.dataItem.index);
});
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
height:97vh;
}
#chartdiv {
width: 100%;
height: 100%;
}
<script src="//www.amcharts.com/lib/4/core.js"></script>
<script src="//www.amcharts.com/lib/4/charts.js"></script>
<script src="//www.amcharts.com/lib/4/themes/animated.js">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<p>Notice that Japan has 0 visits so it is removed</p>
<div id="chartdiv"></div>