在amCharts工具提示中添加图标

时间:2019-02-25 22:15:44

标签: javascript html css html5 amcharts

我已经使用amCharts Javascript库创建了一个简单的图表。它有一个简单的工具提示,它负责显示每个条形的值(用户的积分)。

这是我在说的:

Image can't be loaded!

现在,我想走得更远,在工具提示中的每个值后面显示一个小图像或图标。像这样的东西:

enter image description here

这里是说明我们如何处理创建Tooltips with rich HTML content的链接。

不幸的是,由于它的复杂性和我的经验不足,我无法处理它。但是我敢肯定应该有一种存档方式...

这是我的Javascript代码:

// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end

// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
var myVar = { 
    Red: "#f72c69",
    Normal: "#6887cc",
};

var userA_points = 22;
var userB_points = 20;
var userC_points = 6;
var members_points = 12;
var max_points = Math. max(userA_points, userB_points, userC_points, members_points);
var maxPlus = (max_points + 10)
maxPlus = Math.min(200, Math.max(0, maxPlus));
console.log(maxPlus);

if(userA_points < members_points){
    var colorCompare = myVar.Red
} else {
    var colorCompare = myVar.Normal
}


// Add data
chart.data = [{
    "name": "Damon",
    "points": userA_points,
    "color": "#5479f2",
    "bullet": "https://www.scripturaengage.com/wp-content/uploads/2017/05/Profile-Picture-Koen-VanGeert-circle-ScripturaEngage.png",
    "bullet2": "Diamonds.png"
}, {
    "name": "filip",
    "points": userB_points,
    "color": "#5479f2",
    "bullet": "https://www.scripturaengage.com/wp-content/uploads/2017/05/Profile-Picture-Pauline-Suy-circle-ScripturaEngage.png",
    "bullet2": "Diamonds.png"
}, {
    "name": "Patrick",
    "points": userC_points,
    "color": "#5479f2",
    "bullet": "https://www.scripturaengage.com/wp-content/uploads/2017/05/Profile-Picture-Pauline-Suy-circle-ScripturaEngage.png",
    "bullet2": "Diamonds.png"
}, {
    "name": "All Members",
    "points": members_points,
    "color": colorCompare,
    "bullet": "Members.png",
    "bullet2": "Diamonds.png"
}];

// Create axes
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "name";
categoryAxis.renderer.grid.template.disabled = true;
categoryAxis.renderer.minGridDistance = 30;
categoryAxis.renderer.inside = true;
categoryAxis.renderer.labels.template.fill = am4core.color("#fff");
categoryAxis.renderer.labels.template.fontSize = 20;

var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.renderer.grid.template.strokeDasharray = "4,4";
valueAxis.renderer.labels.template.disabled = false;
valueAxis.renderer.labels.template.fontSize = 15;
valueAxis.renderer.labels.template.fontWeight = "800";

valueAxis.min = 0;
valueAxis.max = maxPlus;
valueAxis.strictMinMax = true;  
valueAxis.renderer.minGridDistance = 25;

// Do not crop bullets
chart.maskBullets = false;

// Remove padding
chart.paddingBottom = 0;
chart.paddingTop = 50;
// Create series
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueY = "points";
series.dataFields.categoryX = "name";
series.columns.template.propertyFields.fill = "color";
series.columns.template.propertyFields.stroke = "color";
series.columns.template.column.cornerRadiusTopLeft = 25;
series.columns.template.column.cornerRadiusTopRight = 25;
series.columns.template.tooltipText = "{categoryX}: [bold]{valueY}[/b]";


// Add bullets
var bullet = series.bullets.push(new am4charts.Bullet());
var image = bullet.createChild(am4core.Image);
image.width = 120;
image.height = 120;
image.horizontalCenter = "middle";
image.verticalCenter = "bottom";
image.dy = 70;
image.y = am4core.percent(100);
image.propertyFields.href = "bullet";
image.tooltipText = series.columns.template.tooltipText;
image.propertyFields.fill = "color";
image.filters.push(new am4core.DropShadowFilter());

// Add bullets
var bullet2 = series.bullets.push(new am4charts.Bullet());
var image = bullet2.createChild(am4core.Image);
image.width = 60;
image.height = 60;
image.horizontalCenter = "middle";
image.verticalCenter = "bottom";
image.dy = 60;
image.dx = 50;
image.y = am4core.percent(100);
image.propertyFields.href = "bullet2";
image.tooltipText = series.columns.template.tooltipText;
image.propertyFields.fill = "color";
image.filters.push(new am4core.DropShadowFilter());

HTML代码:

<!-- Resources -->
<script language="JavaScript" src="amcharts4/core.js"></script>
<script language="JavaScript" src="amcharts4/charts.js"></script>
<script language="JavaScript" src="amcharts4/themes/animated.js"></script>

<div id="chartdiv"></div>
  <link rel="stylesheet" href="Style.css">
  <script src="Script.js"></script>

CSS代码:

#chartdiv {
  width: 1500px;
  height: 750px;
}

body {  
    margin: 90 100 100 20;
    background-color: transparent;
    overflow:hidden;
  font-family: "Helvetica Neue";
  font-weight: 800;
  src: url(helveticaneue-ultrathin.woff);   
 }

任何解决方案都值得赞赏...

1 个答案:

答案 0 :(得分:1)

您应该使用tooltipHTML而不是tooltipText。在那里,您可以使用HTML设计工具提示。要添加图像,只需使用<img>标签:

series.columns.template.tooltipHTML = "<img src='your-image.png' style='vertical-align:bottom; margin-right: 10px; width:21px; height:21px;'><span style='font-size:14px; color:#000000;'><b>POINTS {valueY.value}</b></span><br><img src='your-image.png' style='vertical-align:bottom; margin-right: 10px; width:21px; height:21px;'><span style='font-size:14px; color:#000000;'><b>Game overs: {gameOvers}</b></span>";

我创建了一个code pen作为参考,但是大多数图像在此处均无效。