【问题标题】:Overlaying two AmCharts叠加两个 AmCharts
【发布时间】:2017-06-01 18:19:20
【问题描述】:

我有一个要构建的图表,它有一个散点图,然后是四行。这四行将来自完全不同的数据集,具有相同的 x min max 和 y min max 但结构不同。如何使用固定的 xmin xmax ymin ymax 创建一个图表,然后创建另一个图表,并让一个覆盖另一个。

基本上,我想要一张图表,其中包含两个完全不同的数据集,它们具有相同的域和范围,并保持两者的交互性。我正在尝试将数据集作为字段映射,但运气不佳。我不完全确定我了解字段映射在做什么。我发现的一个例子:

  "dataSets": [ {
    "title": "first data set",
    "fieldMappings": [ {
      "fromField": "value",
      "toField": "value"
  }, {
      "fromField": "volume",
      "toField": "volume"
  } ],
    "dataProvider": chartData1,
    "categoryField": "date"
}, {
    "title": "second data set",
    "fieldMappings": [ {
      "fromField": "value",
      "toField": "value"
  }, {
      "fromField": "volume",
      "toField": "volume"
  } ],
    "dataProvider": chartData2,
    "categoryField": "date"
}],

我现在正在使用数据加载器,如果我可以在一个图表中完成所有操作,我可以使用 ajax 作为另一种选择一次读取两个数据集。所以我也不确定如何在这个例子中使用数据加载器。因此,如果有人知道它是如何工作的,那也会有所帮助。

谢谢!

【问题讨论】:

  • 它有助于解决您的问题吗?

标签: javascript d3.js amcharts


【解决方案1】:

您可以使用简单的 CSS 来实现。

position: absolute,一些定位坐标top&left,以及尺寸width&height。

z-index 来决定什么是最重要的。

而且它们都是透明的背景。

剩下的只是图表的配置JSON中的配置。

AmCharts.makeChart("chartdiv", {
  "type": "serial",
	"autoMargins": false,
	"marginBottom": 0,
	"marginLeft": 0,
	"marginRight": 0,
	"marginTop": 0,
  "categoryField": "category",
  "startDuration": 1,
  "categoryAxis": {
    "gridPosition": "start"
  },
  "trendLines": [],
  "graphs": [{
      "fillAlphas": 1,
      "fillColors": "#4DCD11",
      "balloonText": "[[title]] of [[category]]:[[value]]",
      "fillAlphas": 1,
      "id": "AmGraph-1",
      "title": "graph 1",
      "type": "column",
      "valueField": "column-1"
    },
    {
      "fillAlphas": 1,
      "fillColors": "#070AB5",
      "balloonText": "[[title]] of [[category]]:[[value]]",
      "fillAlphas": 1,
      "id": "AmGraph-2",
      "title": "graph 2",
      "type": "column",
      "valueField": "column-2"
    }
  ],
  "guides": [],
  "allLabels": [],
  "balloon": {},
  "titles": [{
    "id": "Title-1",
    "size": 15,
    "text": " "
  }],
  "dataProvider": [{
      "category": "1",
      "column-1": 8,
      "column-2": 5
    },
    {
      "category": "2",
      "column-1": 6,
      "column-2": 7
    },
    {
      "category": "3",
      "column-1": 2,
      "column-2": 3
    }
  ]
});

AmCharts.makeChart("chartdiv2", {
  "type": "serial",
  "theme": "light",
	"autoMargins": false,
	"marginBottom": 0,
	"marginLeft": 0,
	"marginRight": 0,
	"marginTop": 0,
  "categoryField": "category",
  "startDuration": 1,
  "categoryAxis": {
    "gridPosition": "start"
  },
  "trendLines": [],
  "graphs": [{
      "balloonText": "[[title]] of [[category]]:[[value]]",
      "id": "AmGraph-1",
      "lineThickness": 6,
      "noStepRisers": true,
      "title": "graph 1",
      "type": "step",
      "valueField": "column-1"
    },
    {
      "balloonText": "[[title]] of [[category]]:[[value]]",
      "id": "AmGraph-2",
      "lineThickness": 6,
      "noStepRisers": true,
      "title": "graph 2",
      "type": "step",
      "valueField": "column-2"
    }
  ],
  "guides": [],
  "valueAxes": [{
    "id": "ValueAxis-1",
    "title": " "
  }],
  "allLabels": [],
  "balloon": {},
  "titles": [{
    "id": "Title-1",
    "size": 15,
    "text": " "
  }],
  "dataProvider": [{
      "category": "1",
      "column-1": 8,
      "column-2": 5
    },
    {
      "category": "2",
      "column-1": 6,
      "column-2": "2"
    },
    {
      "category": "3",
      "column-1": "7",
      "column-2": 3
    },
    {
      "category": "4",
      "column-1": "9",
      "column-2": 3
    },
    {
      "category": "5",
      "column-1": "7",
      "column-2": 1
    },
    {
      "category": "6",
      "column-1": "8",
      "column-2": 2
    },
    {
      "category": "7",
      "column-1": "7",
      "column-2": "6"
    }
  ]
});
#chartdiv,
#chartdiv2 {
  position: absolute;
  background-color: transparent;
}

#chartdiv {
  z-index: 100;
  top: 0;
  left: 0;
  width: 600px;
  height: 400px
}

#chartdiv2 {
  z-index: 120;
  top: 120px;
  left: 100px;
  width: 400px;
  height: 180px
}
<script type="text/javascript" src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script type="text/javascript" src="https://www.amcharts.com/lib/3/serial.js"></script>
<div id="chartdiv"></div>
<div id="chartdiv2"></div>

【讨论】:

  • 谢谢!我现在会试试看,它看起来正是我正在寻找的东西
  • 如果您认为这有帮助,请接受答案,或者编辑问题:)
  • 所以我试过了,但不是我想要的。我想我会编辑这个问题,因为我仍然想要交互性。
【解决方案2】:

D3 应该能够很容易地处理这个问题。您可以使用 d3.request(例如 d3.json)来调用数据(如有必要,在单独的请求中)。然后 d3.scale 将您的最小/最大值设置为 svg 图表上定义的点,然后使用请求的数据将您的散点/线等绘制到这些点。

稍微弄乱坐标轴和工具提示,你应该有一个功能图表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    相关资源
    最近更新 更多