代码之家  ›  专栏  ›  技术社区  ›  TyForHelpDude

删除多个值轴的垂直标签轴amchart

  •  1
  • TyForHelpDude  · 技术社区  · 5 年前

    此外,我的图表中只有一个轴和yaxis值,但有多条线。

    如何定制此amchart示例或找到适合我需要的图表?

    enter image description here

    1 回复  |  直到 5 年前
        1
  •  3
  •   xorspark    5 年前

    实际上,图表中的每个组件都有一个 disabled 可用于隐藏或显示的属性。要去除线条和标签,只需设置 属性设置为true以删除它们,类似于禁用网格的方式:

      valueAxis.renderer.line.disabled = true; //disables axis line
      valueAxis.renderer.labels.template.disabled = true; //disables labels
      valueAxis.renderer.grid.template.disabled = true;  //disables grid
    

    演示:

    // Create chart instance
    var chart = am4core.create("chartdiv", am4charts.XYChart);
    
    // Increase contrast by taking evey second color
    chart.colors.step = 2;
    
    // Add data
    chart.data = generateChartData();
    
    // Create axes
    var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
    
    // Create series
    function createAxisAndSeries(field, name, bullet) {
      var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
      
      var series = chart.series.push(new am4charts.LineSeries());
      series.dataFields.valueY = field;
      series.dataFields.dateX = "date";
      series.strokeWidth = 2;
      series.yAxis = valueAxis;
      series.name = name;
      series.tooltipText = "{name}: [bold]{valueY}[/]";
      series.tensionX = 0.8;
      
      var interfaceColors = new am4core.InterfaceColorSet();
      
      switch(bullet) {
        case "triangle":
          var bullet = series.bullets.push(new am4charts.Bullet());
          bullet.width = 12;
          bullet.height = 12;
          bullet.horizontalCenter = "middle";
          bullet.verticalCenter = "middle";
          
          var triangle = bullet.createChild(am4core.Triangle);
          triangle.stroke = interfaceColors.getFor("background");
          triangle.strokeWidth = 2;
          triangle.direction = "top";
          triangle.width = 12;
          triangle.height = 12;
          break;
        case "rectangle":
          var bullet = series.bullets.push(new am4charts.Bullet());
          bullet.width = 10;
          bullet.height = 10;
          bullet.horizontalCenter = "middle";
          bullet.verticalCenter = "middle";
          
          var rectangle = bullet.createChild(am4core.Rectangle);
          rectangle.stroke = interfaceColors.getFor("background");
          rectangle.strokeWidth = 2;
          rectangle.width = 10;
          rectangle.height = 10;
          break;
        default:
          var bullet = series.bullets.push(new am4charts.CircleBullet());
          bullet.circle.stroke = interfaceColors.getFor("background");
          bullet.circle.strokeWidth = 2;
          break;
      }
      
      valueAxis.renderer.line.disabled = true;
      valueAxis.renderer.labels.template.disabled = true;
      valueAxis.renderer.grid.template.disabled = true;
    }
    
    createAxisAndSeries("visits", "Visits",  "circle");
    createAxisAndSeries("views", "Views",  "triangle");
    createAxisAndSeries("hits", "Hits",  "rectangle");
    
    // Add legend
    chart.legend = new am4charts.Legend();
    
    // Add cursor
    chart.cursor = new am4charts.XYCursor();
    
    // generate some random data, quite different range
    function generateChartData() {
      var chartData = [];
      var firstDate = new Date();
      firstDate.setDate(firstDate.getDate() - 100);
      firstDate.setHours(0, 0, 0, 0);
    
      var visits = 1600;
      var hits = 2900;
      var views = 8700;
    
      for (var i = 0; i < 15; i++) {
        // we create date objects here. In your data, you can have date strings
        // and then set format of your dates using chart.dataDateFormat property,
        // however when possible, use date objects, as this will speed up chart rendering.
        var newDate = new Date(firstDate);
        newDate.setDate(newDate.getDate() + i);
    
        visits += Math.round((Math.random()<0.5?1:-1)*Math.random()*10);
        hits += Math.round((Math.random()<0.5?1:-1)*Math.random()*10);
        views += Math.round((Math.random()<0.5?1:-1)*Math.random()*10);
    
        chartData.push({
          date: newDate,
          visits: visits,
          hits: hits,
          views: views
        });
      }
      return chartData;
    }
    #chartdiv {
      width: 100%;
      height: 500px;
    }
    <script src="https://www.amcharts.com/lib/4/core.js"></script>
    <script src="https://www.amcharts.com/lib/4/charts.js"></script>
    <div id="chartdiv"></div>
        2
  •  0
  •   Slava Rozhnev    3 年前

    enter image description here

    多个值轴从图表中删除第三个轴。 只有两个轴将显示。