首页 > 其他分享 >amCharts绘制折线图和柱状图混合

amCharts绘制折线图和柱状图混合

时间:2024-05-30 14:02:26浏览次数:20  
标签:01 chart date 柱状图 amCharts 折线图 new root 2012

  • 代码案例
<!DOCTYPE html>
<html>
  <head>
    <script src="https://cdn.amcharts.com/lib/5/index.js"></script>
    <script src="https://cdn.amcharts.com/lib/5/xy.js"></script>
    <script src="https://cdn.amcharts.com/lib/5/themes/Animated.js"></script>
    <style>
        #chartdiv {
          width: 650PX;
          height: 280px;
        }
    </style>
  </head>
  <body>
    <div id="chartdiv"></div>
    
    <script>
        am5.ready(function() {
        
        var data = [{
          "date": "2012-01-01",
          "distance": 227,
          "townName": "New York",
          "townSize": 12,       // 气泡大小
          "latitude": 40.71,
          "duration": 408       // 持续时间
        }, {
          "date": "2012-01-02",
          "distance": 371,
          "townName": "Washington",
          "townSize": 7,
          "latitude": 38.89,
          "duration": 482
        }, {
          "date": "2012-01-03",
          "distance": 433,
          "townName": "Wilmington",
          "townSize": 3,
          "latitude": 34.22,
          "duration": 562
        }, {
          "date": "2012-01-04",
          "distance": 345,
          "townName": "Jacksonville",
          "townSize": 3.5,
          "latitude": 30.35,
          "duration": 379
        }, {
          "date": "2012-01-05",
          "distance": 480,
          "townName": "Miami",
          "townSize": 5,
          "latitude": 25.83,
          "duration": 501
        }, {
          "date": "2012-01-06",
          "distance": 386,
          "townName": "Tallahassee",
          "townSize": 3.5,
          "latitude": 30.46,
          "duration": 443
        }, {
          "date": "2012-01-07",
          "distance": 348,
          "townName": "New Orleans",
          "townSize": 5,
          "latitude": 29.94,
          "duration": 405
        }, {
          "date": "2012-01-08",
          "distance": 238,
          "townName": "Houston",
          "townSize": 8,
          "latitude": 29.76,
          "duration": 309
        }, {
          "date": "2012-01-09",
          "distance": 218,
          "townName": "Dalas",
          "townSize": 8,
          "latitude": 32.8,
          "duration": 287
        }, {
          "date": "2012-01-10",
          "distance": 349,
          "townName": "Oklahoma City",
          "townSize": 5,
          "latitude": 35.49,
          "duration": 485
        }, {
          "date": "2012-01-11",
          "distance": 603,
          "townName": "Kansas City",
          "townSize": 5,
          "latitude": 39.1,
          "duration": 890
        }, {
          "date": "2012-01-12",
          "distance": 534,
          "townName": "Denver",
          "townSize": 9,
          "latitude": 39.74,
          "duration": 810
        }, {
          "date": "2012-01-13",
          "townName": "Salt Lake City",
          "townSize": 6,
          "distance": 425,
          "duration": 670,
          "latitude": 40.75,
          "dashLength": 8,
          "alpha": 0.4
        }, {
          "date": "2012-01-14",
          "latitude": 36.1,
          "duration": 470,
          "townName": "Las Vegas"
        }, {
          "date": "2012-01-15"
        }, {
          "date": "2012-01-16"
        }, {
          "date": "2012-01-17"
        }];
        
        // Create root element
        // https://www.amcharts.com/docs/v5/getting-started/#Root_element
        var root = am5.Root.new("chartdiv");
        
        // Set themes
        // https://www.amcharts.com/docs/v5/concepts/themes/
        root.setThemes([
          am5themes_Animated.new(root)
        ]);
        
        
        // Create chart
        // https://www.amcharts.com/docs/v5/charts/xy-chart/
        var chart = root.container.children.push(am5xy.XYChart.new(root, {
          panX: false,
          panY: false,
          wheelY: "none"
        }));
        
        chart.zoomOutButton.set("forceHidden", true);
        
        chart.get("colors").set("step", 2);
        
        // Create axes
        // https://www.amcharts.com/docs/v5/charts/xy-chart/axes/
        var xAxis = chart.xAxes.push(am5xy.DateAxis.new(root, {
          baseInterval: { timeUnit: "day", count: 1 },
          renderer: am5xy.AxisRendererX.new(root, { 
            minGridDistance: 70,
            minorGridEnabled: true
          }),
          tooltip: am5.Tooltip.new(root, {})
        }));
        
        
        var distanceAxisRenderer = am5xy.AxisRendererY.new(root, {});
        distanceAxisRenderer.grid.template.set("forceHidden", true);
        var distanceAxis = chart.yAxes.push(am5xy.ValueAxis.new(root, {
          renderer: distanceAxisRenderer,
          tooltip: am5.Tooltip.new(root, {})
        }));
        
        var latitudeAxisRenderer = am5xy.AxisRendererY.new(root, {});
        latitudeAxisRenderer.grid.template.set("forceHidden", true);
        var latitudeAxis = chart.yAxes.push(am5xy.ValueAxis.new(root, {
          renderer: latitudeAxisRenderer,
          forceHidden: true
        }));
        
        var durationAxisRenderer = am5xy.AxisRendererY.new(root, {
          opposite: true
        });
        durationAxisRenderer.grid.template.set("forceHidden", true);
        var durationAxis = chart.yAxes.push(am5xy.DurationAxis.new(root, {
          baseUnit:"minute",
          renderer: durationAxisRenderer,
          extraMax:0.3
        }));
        
        // Create series
        // https://www.amcharts.com/docs/v5/charts/xy-chart/series/
        var distanceSeries = chart.series.push(am5xy.ColumnSeries.new(root, {
          xAxis: xAxis,
          yAxis: distanceAxis,
          valueYField: "distance",
          valueXField: "date",
          tooltip:am5.Tooltip.new(root, {
            labelText:"{valueY} miles"
          })
        }));
        
        distanceSeries.data.processor = am5.DataProcessor.new(root, {
          dateFields: ["date"],
          dateFormat: "yyyy-MM-dd"
        });
        
        var latitudeSeries = chart.series.push(am5xy.LineSeries.new(root, {
          xAxis: xAxis,
          yAxis: latitudeAxis,
          valueYField: "latitude",
          valueXField: "date",
          tooltip:am5.Tooltip.new(root, {
            labelText:"latitude: {valueY} ({townName})"
          })  
        }));
        
        latitudeSeries.strokes.template.setAll({ strokeWidth: 2 });
        
        // Add circle bullet
        // https://www.amcharts.com/docs/v5/charts/xy-chart/series/#Bullets
        latitudeSeries.bullets.push(function() {
          var graphics = am5.Circle.new(root, {
            strokeWidth: 2,
            radius: 5,
            stroke: latitudeSeries.get("stroke"),
            fill: root.interfaceColors.get("background"),
          });
        
          graphics.adapters.add("radius", function(radius, target) {
            return target.dataItem.dataContext.townSize;
          })
        
          return am5.Bullet.new(root, {
            sprite: graphics
          });
        });
        
        var durationSeries = chart.series.push(am5xy.LineSeries.new(root, {
          xAxis: xAxis,
          yAxis: durationAxis,
          valueYField: "duration",
          valueXField: "date",
          tooltip:am5.Tooltip.new(root, {
            labelText:"duration: {valueY.formatDuration()}"
          }) 
        }));
        
        durationSeries.strokes.template.setAll({ strokeWidth: 2 });
        
        // Add circle bullet
        // https://www.amcharts.com/docs/v5/charts/xy-chart/series/#Bullets
        durationSeries.bullets.push(function() {
          var graphics = am5.Rectangle.new(root, {
            width:10, 
            height:10,
            centerX:am5.p50,
            centerY:am5.p50,
            fill: durationSeries.get("stroke")
          });
        
          return am5.Bullet.new(root, {
            sprite: graphics
          });
        });
        
        // Add cursor
        // https://www.amcharts.com/docs/v5/charts/xy-chart/cursor/
        chart.set("cursor", am5xy.XYCursor.new(root, {
          xAxis: xAxis,
          yAxis: distanceAxis
        }));
        
        
        distanceSeries.data.setAll(data);
        latitudeSeries.data.setAll(data);
        durationSeries.data.setAll(data);
        xAxis.data.setAll(data);
        
        // Make stuff animate on load
        // https://www.amcharts.com/docs/v5/concepts/animations/
        distanceSeries.appear(1000);
        chart.appear(1000, 100);
        
        }); // end am5.ready()
    </script>       
  </body>
</html>
  • 效果图

标签:01,chart,date,柱状图,amCharts,折线图,new,root,2012
From: https://www.cnblogs.com/dogleftover/p/18222199

相关文章

  • amCharts绘制堆叠面积图
    代码案例<!DOCTYPEhtml><html><head><scriptsrc="https://cdn.amcharts.com/lib/5/index.js"></script><scriptsrc="https://cdn.amcharts.com/lib/5/xy.js"></script><scriptsrc=&qu......
  • amCharts绘制折线图比较
    代码案例<!DOCTYPEhtml><html><head><scriptsrc="https://cdn.amcharts.com/lib/5/index.js"></script><scriptsrc="https://cdn.amcharts.com/lib/5/xy.js"></script><scriptsrc=&qu......
  • AnyChart绘制折线图
    代码案例<html><head><scriptsrc="https://cdn.anychart.com/releases/v8/js/anychart-base.min.js"></script><scriptsrc="https://cdn.anychart.com/releases/v8/js/anychart-ui.min.js"></script......
  • 封装通用 ECharts 图表组件(四):分格柱状图实现
    在数据可视化的世界中,ECharts以其强大的功能和灵活性,成为了开发者们的首选图表库之一。继我们之前探讨的环形图封装之后,本文将带领大家进入另一个实用且美观的图表类型——分格柱状图的封装实现。分格柱状图简介分格柱状图是一种特殊的柱状图,它通过将每个柱子分割成多个小格......
  • 统计不同文件夹中的文件数量,并绘制相应的柱状图。
    一、数据类型每个文件夹下都是这种文件,虽然可以通过手动数出来了解文件数量,但为了更直观地看到每个文件夹的文件数量,可以使用图表来表示,这样会更加清晰。效果展示:  二、代码实现 importosimportmatplotlib.pyplotaspltfolder_names=['0','1','2','3']......
  • matplotlib-折线图的常用操作合集
    序言:感悟:复杂的折线图,都是由基础的折线图慢慢加工绘制而成,我们通过绘制出基本折线图,然后分析需要添加的功能,慢慢润色图形即可完成复杂的图形绘制。折线图:以折线的上升或下降来表示统计数量的增减变化的统计图特点:能够显示数据的变化趋势,反映事物的变化情况。(变化)我们以一个......
  • ZingChart折线图
    代码案例,参考<!DOCTYPEhtml><html><head><metacharset="utf-8"><title>ZingSoftDemo</title><scriptnonce="undefined"src="https://cdn.zingchart.com/zingchart.min.js"></scrip......
  • echarts-柱状图翻转 适合排名展示 越小越大,越大越小
    先上效果(折线图也可,看代码中标注*的位置):代码:dataList=[1,2,9,8,10,14,3];//初始数值*dataList1=[];//翻转后的数值*vardd=2;//系数用来防止计算后为0不显示*maxValue=dataList[0];//*maxValue=Math.max(...dataList);//取最大值*[dataList1]......
  • amCharts折线图
    代码案例,参考<!DOCTYPEhtml><html><head><scriptsrc="https://cdn.amcharts.com/lib/5/index.js"></script><scriptsrc="https://cdn.amcharts.com/lib/5/xy.js"></script><scriptsrc=......
  • 力扣-84. 柱状图中最大的矩形
    1.题目介绍题目地址(84.柱状图中最大的矩形-力扣(LeetCode))https://leetcode.cn/problems/largest-rectangle-in-histogram/题目描述给定n个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为1。求在该柱状图中,能够勾勒出来的矩形的最大面积。 示......