amcharts添加事件

博客介绍了handleClick方法,它是点击事件触发的函数,可用于实现如点击柱子跳转到二级图形等操作。还指出若测试发现图形加载不稳定,可能是“AmCharts.ready(function() {})”函数名定义有问题,可去掉该定义解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>amcharts</title>
</head>
<body>
<script src="http://www.amcharts.com/lib/amcharts.js" type="text/javascript"></script>
<script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>amchars点击事件</title>
<!-- 可以实现点击跳转到另外的一个图表 -->
</head>
<body>
<script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
<!-- 柱形图的 -->
<!-- <script src="http://www.amcharts.com/lib/amcharts.js" type="text/javascript"></script> -->
<!-- 饼图的 -->
<script src="amcharts/amcharts.js" type="text/javascript"></script>
<script src="amcharts/pie.js" type="text/javascript"></script>
<script src="/amcharts/serial.js" type="text/javascript"></script>


<div id="chartdiv" style="width: 100%; height: 440px; background-color:#eeeeee;"></div>
<div id="chartdiv1" style="width: 100%; height: 440px; display:none; background-color:#eeeeee;" ></div>
<script type="text/javascript">
    /* 柱形图 */
    var chart;
    var chartData = [{
        country: "USA",
        visits: 4025},
    {
        country: "China",
        visits: 1882},
    {
        country: "Japan",
        visits: 1809},
    {
        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},
    {
        country: "Brazil",
        visits: 395},
    {
        country: "Italy",
        visits: 386},
    {
        country: "Australia",
        visits: 384},
    {
        country: "Taiwan",
        visits: 338},
    {
        country: "Poland",
        visits: 328
    }];

    /* 饼图 */
     var chart1;
   var chartData1 = [
       {
           "country": "大宝",
           "visits": 6000
       },
       {
           "country": "二宝",
           "visits": 1882
       },
       {
           "country": "三宝",
           "visits": 1809
       },
       {
           "country": "四宝",
           "visits": 1322
       },
       {
           "country": "五宝",
           "visits": 1122
       },
       {
           "country": "六宝",
           "visits": 1114
       },
       {
           "country": "七宝",
           "visits": 984
       },
       {
           "country": "八宝",
           "visits": 711
       }
   ];


    /* 柱形图点击事件的方法 */
    function handleClick(event)
    {
        alert(event.item.category + ": " + event.item.values.value);
        $("#chartdiv1").css("display","block");
        /* 饼图 */
        chart1 = new AmCharts.AmPieChart();

        chart1.addTitle("666666", 16);
        chart1.dataProvider = chartData1;
        chart1.titleField = "country";   //分类类别项
        chart1.valueField = "visits";  //比例值
        chart1.sequencedAnimation = true;
        chart1.startEffect = "elastic";
        chart1.innerRadius = "30%";
        chart1.startDuration =2;  //饼图的渲染时间,时间越长,动画越长,展示的准备时间越久
        chart1.labelRadius = 10;  //饼图上标签,值越大,饼图越小,说明的标签指示线越长
        chart1.balloonText = "[[title]]<br/><span><b>[[value]]</b> ([[percents]]%)</span>";
        chart1.depth3D = 10;  //3D效果的立体度 ,值越大立体感越强
        chart1.angle = 20;  //角度占的分额基数,值大则显示的饼图倍数就会扩大,但是值不能超过限定的最大值,否则会变小

        chart1.write("chartdiv1");

    }
    /* 饼图 */
     var chart1;
    var chartData1 = [
        {
            "country": "大宝",
            "visits": 6000
        },
        {
            "country": "二宝",
            "visits": 1882
        },
        {
            "country": "三宝",
            "visits": 1809
        },
        {
            "country": "四宝",
            "visits": 1322
        },
        {
            "country": "五宝",
            "visits": 1122
        },
        {
            "country": "六宝",
            "visits": 1114
        },
        {
            "country": "七宝",
            "visits": 984
        },
        {
            "country": "八宝",
            "visits": 711
        }
    ];

        /* 柱形图 */
        chart = new AmCharts.AmSerialChart();
        chart.dataProvider = chartData;
        chart.categoryField = "country";
        chart.startDuration = 1;
        chart.addListener("clickGraphItem", handleClick);
        var categoryAxis = chart.categoryAxis;
        categoryAxis.labelRotation = 90;
        categoryAxis.gridPosition = "start";
        var graph = new AmCharts.AmGraph();
        graph.valueField = "visits";
        graph.balloonText = "[[category]]: [[value]]";
        graph.type = "column";
        graph.lineAlpha = 0;
        graph.fillAlphas = 0.8;
        chart.addGraph(graph);
        chart.write("chartdiv");


</script>
</body>
</html>

handleClick方法就是点击事件触发的函数,可以通过这个函数做一些你想做的事,例如点击柱子跳到另一个二级图型,另外,如果你做完后测试发现图型有时加载缓慢,有时甚至没有加载,时灵时不灵的,应该是这个方法”AmCharts.ready(function() {}”,这个函数名定义有问题,如本文一样可以去掉

xml <!-- [xml] (xml / csv) 数据类型xml/csv--> ; <!-- 如果使用csv作为数据的话,需要使用这个属性;表示文件数据分隔符,(平常以";"和","为主) [;] (string) csv file data separator (you need it only if you are using csv file for your data) --> 1 <!-- 如果使用的是csv数据,可以设置跳过几行再显示数据,默认为0表示csv中的数据全部显示,大于n(n>0);表示前面n行都不显示[0] (Number) if you are using csv data type, you can set the number of rows which should be skipped here --> <!-- 设置系统中的字体[Arial] (font name) use device fonts, such as Arial, Times New Roman, Tahoma, Verdana... --> <!-- 设置所有文本的大小,默认为11,具体的文本的字体大小也可以在下面的设置中设置[11] (Number) text size of all texts. Every text size can be set individually in the settings below --> <!-- 同上[#000000] (hex color code) main text color. Every text color can be set individually in the settings below--> . <!-- 小数分隔符,默认为[,]注:该属性只是用来显示,而在csv数据文件中,必须使用[.] (string) decimal separator. Note, that this is for displaying data only. Decimals in data xml file must be separated with a dot --> <!-- 千位分隔符,默认为空[ ] (string) thousand separator. use "none" if you don't want to separate --> 3 <!-- 如果百分数格式的数字,后面的小数位小于该属性的值,则在小数后面加0补充。如54.2%,该属性设置为3,那么显示的效果为54.200%。[] (Number) if your value has less digits after decimal then is set here, zeroes will be added --> <!--设置科学记数法的最小值 [0.000001] If absolute value of your number is equal or less then scientific_min, this number will be form
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值