在鼠标悬停时显示单独的工具提示(顶部/中间/底部)错误amchart的上下点

时间:2016-01-15 10:16:08

标签: amcharts

我正在使用错误amchart所以我想在鼠标悬停时显示顶部错误,中间错误和每个错误字段的底部错误的ballon(工具提示)。鼠标离开时隐藏。以下是错误amchart链接https://www.amcharts.com/demos/error-chart/

2 个答案:

答案 0 :(得分:0)

我真的不确定这是否是你要求的。

您可以使用balloonFunction更改气球的文字。

"graphs": [ {
     "balloonFunction": function(item) {
       var data = item.dataContext;
       return "high:<b>" + (data.value + data.error)
           + "</b><br>low:<b>" + (data.value - data.error)
           + "</b><br>value:<b>" + data.value
           + "</b><br>error:<b>" + data.error + "</b>";
     }
[

看看这个demo

答案 1 :(得分:0)

您可以通过添加一些自定义代码来解决此问题,该代码会为顶部和底部错误值创建单独的图形。您将需要三个图表,因为图表每个图表只显示一个气球。

这是一个快速的插件&#34;我掀起了那种自动做到的事情:

/**
 * amCharts plugin: show three balloons for each error data point
 * The plugin expects "showAllBalloons" to be set to true for the graph
 * you want this to be applied to
 */
AmCharts.addInitHandler(function(chart) {

  // find graphs that need to be augmented with additional balloons
  for(var i = 0; i < chart.graphs.length; i++) {
    var g = chart.graphs[i];
    if (g.showAllBalloons === true) {
      for(var x = 0; x < chart.dataProvider.length; x++) {
        var dp = chart.dataProvider[x];
        var value = dp[g.valueField];
        var error = dp[g.errorField];
        dp[g.valueField + "Top"] = value + error / 2;
        dp[g.valueField + "Bottom"] = value - error / 2;
      }

      // add additional graph for top and bottom values
      var graph = new AmCharts.AmGraph();
      graph.valueField = g.valueField + "Top";
      graph.lineColor = g.lineColor;
      graph.visibleInLegend = false;
      graph.lineAlpha = 0;
      chart.addGraph(graph);

      var graph = new AmCharts.AmGraph();
      graph.valueField = g.valueField + "Bottom";
      graph.lineColor = g.lineColor;
      graph.visibleInLegend = false;
      graph.lineAlpha = 0;
      chart.addGraph(graph);
    }
  }

}, ["serial"]);

这是一个完整的代码:

&#13;
&#13;
/**
 * amCharts plugin: show three balloons for each error data point
 * The plugin expects "showAllBalloons" to be set to true for the graph
 * you want this to be applied to
 */
AmCharts.addInitHandler(function(chart) {
  
  // find graphs that need to be augmented with additional balloons
  for(var i = 0; i < chart.graphs.length; i++) {
    var g = chart.graphs[i];
    if (g.showAllBalloons === true) {
      for(var x = 0; x < chart.dataProvider.length; x++) {
        var dp = chart.dataProvider[x];
        var value = dp[g.valueField];
        var error = dp[g.errorField];
        dp[g.valueField + "Top"] = value + error / 2;
        dp[g.valueField + "Bottom"] = value - error / 2;
      }
      
      // add additional graph for top and bottom values
      var graph = new AmCharts.AmGraph();
      graph.valueField = g.valueField + "Top";
      graph.lineColor = g.lineColor;
      graph.visibleInLegend = false;
      graph.lineAlpha = 0;
      chart.addGraph(graph);
      
      var graph = new AmCharts.AmGraph();
      graph.valueField = g.valueField + "Bottom";
      graph.lineColor = g.lineColor;
      graph.visibleInLegend = false;
      graph.lineAlpha = 0;
      chart.addGraph(graph);
    }
  }
  
}, ["serial"]);

var chart = AmCharts.makeChart( "chartdiv", {
  "type": "serial",
  "theme": "light",
  "dataProvider": [ {
    "year": 2005,
    "value": 11.5,
    "error": 10
  }, {
    "year": 2006,
    "value": 26.2,
    "error": 5
  }, {
    "year": 2007,
    "value": 30.1,
    "error": 5
  }, {
    "year": 2008,
    "value": 29.5,
    "error": 7
  }, {
    "year": 2009,
    "value": 24.6,
    "error": 10
  } ],
  "balloon": {
    "textAlign": "left"
  },
  "valueAxes": [ {
    "id": "v1",
    "axisAlpha": 0
  } ],
  "startDuration": 1,
  "graphs": [ {
    "balloonText": "value:<b>[[value]]</b><br>error:<b>[[error]]</b>",
    "bullet": "yError",
    "bulletSize": 10,
    "lineColor": "#67b7dc",
    "errorField": "error",
    "lineThickness": 2,
    "valueField": "value",
    "bulletAxis": "v1",
    "fillAlphas": 0,
    "showAllBalloons": true
  }, {
    "bullet": "round",
    "bulletBorderAlpha": 1,
    "bulletBorderColor": "#FFFFFF",
    "lineAlpha": 0,
    "lineThickness": 2,
    "showBalloon": false,
    "valueField": "value"
  } ],
  "chartCursor": {
    "cursorAlpha": 0,
    "cursorPosition": "mouse",
    "graphBulletSize": 1,
    "zoomable": false
  },
  "categoryField": "year",
  "categoryAxis": {
    "gridPosition": "start",
    "axisAlpha": 0
  }
} );
&#13;
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/serial.js"></script>
<script src="https://www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv" style="height: 300px;"></div>
&#13;
&#13;
&#13;