彩票走势图

amCharts使用任意文本替代标签教程

原创|使用教程|编辑:龚雪|2014-09-10 09:59:23.000|阅读 1286 次

概述:本文为amCharts使用任意文本替代标签教程。amcharts的ValueAxis中有一个属性叫labelFunction。在展现标签之前如果为该属性设置一些功能,将会呼出该功能、传递值、格式化值以及数值轴参考作为参数,该功能将会返回代替原来默认值的字符串。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

尽管你可以像格式化时长一样格式化值轴标签,在值的左右添加单元。不过,你可能需要更多的功能,比如,用字符串代替值的数值轴,或者为分类轴标签添加文本。

amcharts的ValueAxis中有一个属性叫labelFunction。在展现标签之前如果为该属性设置一些功能,将会呼出该功能、传递值、格式化值以及数值轴参考作为参数,该功能将会返回代替原来默认值的字符串。

<img class="img-thumbnail alt=" amcharts="" 用任意文本替代标签"="" data-cke-saved-src="//image.pclwef.cn/images/article/2014/amcharts_Replacing_axis_labels_with_arbitrary_text_.jpg" src="//image.pclwef.cn/images/article/2014/amcharts_Replacing_axis_labels_with_arbitrary_text_.jpg">

var chart;

var chartData = [
    {
        "category": "Evaluation",
        "excelent": 20,
        "good": 20,
        "average": 20,
        "poor": 20,
        "bad": 20,
        "limit": 78,
        "full": 100,
        "bullet": 65
    }
];


AmCharts.ready(function () {
    
    // FIRST BULLET CHART
    // bullet chart is a simple serial chart
    chart = new AmCharts.AmSerialChart();
    chart.dataProvider = chartData;
    chart.categoryField = "category";
    chart.rotate = true; // if you want vertical bullet chart, set rotate to false
    chart.columnWidth = 1;
    chart.startDuration = 1;
    
    // AXES
    // category
    var categoryAxis = chart.categoryAxis;
    categoryAxis.gridAlpha = 0;
    
    // value
    var valueAxis = new AmCharts.ValueAxis();
    valueAxis.labelFunction = formatValue;
    valueAxis.maximum = 100;
    valueAxis.axisAlpha = 1;
    valueAxis.gridAlpha = 0;
    valueAxis.stackType = "regular"; // we use stacked graphs to make color fills
    chart.addValueAxis(valueAxis);
    
    // this graph displays the short dash, which usually indicates maximum value reached.
    var graph = new AmCharts.AmGraph();
    graph.valueField = "limit";
    graph.lineColor = "#000000";
    // it's a step line with no risers
    graph.type = "step";
    graph.noStepRisers = true;
    graph.lineAlpha = 1;
    graph.lineThickness = 3;
    graph.columnWidth = 0.5; // change this if you want wider dash
    graph.stackable = false; // this graph shouldn't be stacked
    chart.addGraph(graph);
    
    // The following graphs produce color bands
    graph = new AmCharts.AmGraph();
    graph.valueField = "excelent";
    graph.lineColor = "#19d228";
    graph.showBalloon = false;
    graph.type = "column";
    graph.fillAlphas = 0.8;
    chart.addGraph(graph);
    
    graph = new AmCharts.AmGraph();
    graph.valueField = "good";
    graph.lineColor = "#b4dd1e";
    graph.showBalloon = false;
    graph.type = "column";
    graph.fillAlphas = 0.8;
    chart.addGraph(graph);
    
    graph = new AmCharts.AmGraph();
    graph.valueField = "average";
    graph.lineColor = "#f4fb16";
    graph.showBalloon = false;
    graph.type = "column";
    graph.fillAlphas = 0.8;
    chart.addGraph(graph);
    
    graph = new AmCharts.AmGraph();
    graph.valueField = "poor";
    graph.lineColor = "#f6d32b";
    graph.showBalloon = false;
    graph.type = "column";
    graph.fillAlphas = 0.8;
    chart.addGraph(graph);
    
    graph = new AmCharts.AmGraph();
    graph.valueField = "bad";
    graph.lineColor = "#fb7116";
    graph.showBalloon = false;
    graph.type = "column";
    graph.fillAlphas = 0.8;
    chart.addGraph(graph);
    
    // this is the "bullet" graph - black bar showing current value
    graph = new AmCharts.AmGraph();
    graph.valueField = "bullet";
    graph.lineColor = "#000000";
    graph.type = "column";
    graph.lineAlpha = 1;
    graph.fillAlphas = 1;
    graph.columnWidth = 0.3; // this makes it narrower than color graphs
    graph.stackable = false; // bullet graph should not stack
    graph.clustered = false; // this makes the trick - one column above another
    chart.addGraph(graph);
    
    // WRITE
    chart.write("chartdiv");   
    
});

function formatValue(value, formattedValue, valueAxis){
    if(value === 0){
        return "little";
    }
    else if(value == 50){
        return "so-so";
    }
    else if (value == 100){
        return "a lot!";
    }
    else{
        return "";
    }
}

 


标签:JavaScript 图表amcharts

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn

文章转载自:慧都控件网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP