在Amchart实例顶部添加HTML元素

时间:2019-03-30 20:37:29

标签: javascript css amcharts amcharts4

我在文档中找不到此内容,但是可以在Amchart实例顶部添加自定义div元素吗?

如此:

    <div class="container-fluid px-0 mx-0">
        <div id="chartdiv"></div>
        <ul>
            <li>Thailand</li>
            <li>Myanmar</li>
            <li>Etc...</li>
        </ul>
    </div>

在实例底部显示UL吗?

JS:

    <script src="https://www.amcharts.com/lib/4/core.js"></script>
    <script src="https://www.amcharts.com/lib/4/maps.js"></script>
    <script src="https://www.amcharts.com/lib/4/geodata/worldUltra.js"></script>
    <script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
    <script>

       am4core.useTheme(am4themes_animated);

       var container = am4core.create("chartdiv", am4core.Container);
       container.width = am4core.percent(100);
       container.height = am4core.percent(100);
       container.layout = "vertical";

       // Create map instance
       var chart = container.createChild(am4maps.MapChart);

       // Set map definition
       chart.geodata = am4geodata_worldUltra;

       // Set projection
       chart.projection = new am4maps.projections.Miller();

       // Create map polygon series
       var polygonSeries = chart.series.push(new am4maps.MapPolygonSeries());

       // Exclude Antartica
       polygonSeries.exclude = ["AQ"];

       // Make map load polygon (like country names) data from GeoJSON
       polygonSeries.useGeodata = true;

       // Configure series
       var polygonTemplate = polygonSeries.mapPolygons.template;
       polygonTemplate.tooltipText = "{name}";
       polygonTemplate.fill = am4core.color("#dcdcdc");

       // Create hover state and set alternative fill color
       var hs = polygonTemplate.states.create("hover");
       hs.properties.fill = am4core.color("#a98239");

       chart.events.on("ready", function(ev) {
         chart.zoomToMapObject(polygonSeries.getPolygonById("TH"));
       });

       chart.zoomControl = new am4maps.ZoomControl();
       chart.chartContainer.wheelable = false;



    </script>

如果我错过了文档中的某些内容,我深表歉意-希望有人可以指出正确的方向!

1 个答案:

答案 0 :(得分:2)

AmCharts是基于SVG的,因此chartdiv中的所有内容都由库控制,并且主要包含带有少量HTML的SVG,没有任何包含自定义div对象的本机选项。一种可能的解决方法是使用Label对象并设置其html属性以包括您的HTML代码。请注意,这是使用<foreignObject>来完成的,因此您可能需要注意browser support(如果仍然很重要,则为IE11)。

这是一个在图表顶部创建列表的示例

var label = chart.chartContainer.createChild(am4core.Label);
//label.isMeasured = false; //uncomment to make the label not adjust the rest of the chart elements to accommodate its placement
label.fontSize = 16;
label.x = am4core.percent(5);
label.horizontalCenter = "middle";
label.verticalCenter = "bottom";
label.html = "<ul><li>List item 1</li><li>List item 2</li></ul>";
label.toBack(); //move list to top of the chart area. See: https://www.amcharts.com/docs/v4/concepts/svg-engine/containers/#Ordering_elements

演示:

// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);

// Add data
chart.data = [{
  "category": "Research",
  "value": 450
}, {
  "category": "Marketing",
  "value": 1200
}, {
  "category": "Distribution",
  "value": 1850
}];

// Create axes
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "category";
categoryAxis.renderer.grid.template.location = 0;
//categoryAxis.renderer.minGridDistance = 30;

var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());

// Create series
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueY = "value";
series.dataFields.categoryX = "category";

var label = chart.chartContainer.createChild(am4core.Label);
//label.isMeasured = false; //uncomment to make the label not adjust the rest of the chart elements to accommodate its placement
label.fontSize = 16;
label.x = am4core.percent(5);
label.horizontalCenter = "middle";
label.verticalCenter = "bottom";
label.html = "<ul><li>List item 1</li><li>List item 2</li></ul>";
label.toBack(); //move list to top of the chart area. See: https://www.amcharts.com/docs/v4/concepts/svg-engine/containers/#Ordering_elements
html, body { width: 100%; height: 100%; margin: 0;}
#chartdiv { width: 100%; height: 100%;}
<script src="//www.amcharts.com/lib/4/core.js"></script>
<script src="//www.amcharts.com/lib/4/charts.js"></script>
<div id="chartdiv"></div>