【问题标题】:Trouble gettings CanvasJS to color just the coordinates in the tooltip无法让 CanvasJS 只为工具提示中的坐标着色
【发布时间】:2019-04-27 02:32:36
【问题描述】:

工具提示文本:

10 Dataseries 1: 71 Dataseries 2: 77

我正在尝试获取工具提示,以便 Dataseries 1:Dataseries 2: 保持其当前颜色,而 107177 为红色。我已经尝试过toolTipContent: " x: <span style='\"'color: red;'\"'>{x}</span> y: {y} <br/> name: {name}, label:{label} ",,但它并没有改变任何东西。我确信这是我的一个愚蠢的错误,但我是使用 CanvasJS 的新手,并且无法得到任何工作。 (https://jsfiddle.net/lightmaster/8p3ygwf1/)

var chart = new CanvasJS.Chart("chartContainer", {
  backgroundColor: "RGBA(37, 41, 45, 0.9)",
  animationEnabled: true,

  title: {
    text: " ",
    fontSize: 11,
    fontColor: ' #ccc',
    fontFamily: "arial",
  },

  toolTip: {
    fontStyle: "normal",
    cornerRadius: 4,
    backgroundColor: "RGBA(37, 41, 45, 0.9)",
    toolTipContent: " x: {x} y: {y} <br/> name: {name}, label:{label} ",
    shared: true,
  },

  axisX: {
    gridColor: "RGBA(64, 65, 66, 0.8)",
    labelFontSize: 10,
    labelFontColor: ' #ccc',
    lineThickness: 1,
    gridThickness: 1,
    gridDashType: "dot",
    titleFontFamily: "arial",
    labelFontFamily: "arial",
    interval: "auto",
    intervalType: "hour",
    minimum: 0,
    crosshair: {
      enabled: true,
      snapToDataPoint: true,
      color: "#9aba2f",
      labelFontColor: "#ccc",
      labelFontSize: 14,
      labelBackgroundColor: "#FF8841",
    }
  },

  axisY: {
    title: "Temperature (°F) Recorded",
    titleFontColor: "#ccc",
    titleFontSize: 10,
    titleWrap: false,
    margin: 10,
    lineThickness: 1,
    gridThickness: 1,
    gridDashType: "dot",
    includeZero: false,
    gridColor: "RGBA(64, 65, 66, 0.8)",
    labelFontSize: 11,
    labelFontColor: ' #ccc',
    titleFontFamily: "arial",
    labelFontFamily: "arial",
    labelFormatter: function(e) {
      return e.value.toFixed(0) + " °F ";
    },
    crosshair: {
      enabled: true,
      snapToDataPoint: true,
      color: "#9aba2f",
      labelFontColor: "#fff",
      labelFontSize: 12,
      labelBackgroundColor: "#FF8841",
      valueFormatString: "#0.# °F",
    }
  },

  legend: {
    fontFamily: "arial",
    fontColor: "#ccc",

  },
  data: [
    {
      type: "column",
      dataPoints: [
        { x: 10, y: 71 },
        { x: 20, y: 55 },
        { x: 30, y: 50 },
        { x: 40, y: 65 },
        { x: 50, y: 95 },
        { x: 60, y: 68 },
        { x: 70, y: 28 },
        { x: 80, y: 34 },
        { x: 90, y: 14 }
      ]
    },
    {
      type: "spline",
      dataPoints: [
        { x: 10, y: 77 },
        { x: 20, y: 53 },
        { x: 30, y: 58 },
        { x: 40, y: 61 },
        { x: 50, y: 99 },
        { x: 60, y: 60 },
        { x: 70, y: 20 },
        { x: 80, y: 31 },
        { x: 90, y: 26 }
      ]
    }
  ]
});

chart.render();
<script src="https://cdnjs.cloudflare.com/ajax/libs/canvasjs/1.7.0/canvasjs.min.js"></script>
<br/>
<!-- Just so that JSFiddle's Result label doesn't overlap the Chart -->
<div id="chartContainer" style="height: 360px; width: 100%;"></div>

【问题讨论】:

    标签: javascript php canvasjs


    【解决方案1】:

    在图表级别,您需要content 而不是toolTipContent。您的 toolTipContent 代码当前被忽略,因为这是仅在数据级别使用的属性。以下是您可以直接设置样式的方法,基本上按照您的要求进行:

    toolTip: {
      fontStyle: "normal",
      cornerRadius: 14,
      backgroundColor: "RGBA(37, 41, 45, 0.9)",
      content: "<span style='\"'color: red;'\"'>{x}</span><br/> <span style='\"'color: {color};'\"'>{name}</span> <span style='\"'color: red;'\"'>{y}</span>",
      shared: true,
    }
    

    因为您使用的是shared: true,所以您的x 值将显示两次。如果您不想这样,请查看文档中的 contentFormatter function for Shared toolTip 部分。

    【讨论】:

    • 你的确实有效,但就像你说的那样,它添加了一个额外的x。我查看了文档中的 contentFormatter 并修改了他们的 contentFormatter 代码,以便它显示我想要的信息,但是当我用 &lt;span&gt; 代码替换 &lt;strong&gt; 代码时,它不再是粗体但不会改变颜色:@ 987654322@
    • 没关系,我所要做的就是改变一些引号,这样就不需要转义引号:contentFormatter: function(e){ var str = '&lt;span style="color: red;"&gt;' + e.entries[0].dataPoint.x + '&lt;/span&gt;&lt;br/&gt;'; for (var i = 0; i &lt; e.entries.length; i++){ var temp = '&lt;span style="color: ' + e.entries[i].dataSeries.color + ';"&gt;' + e.entries[i].dataSeries.name + '&lt;/span&gt; &lt;span style="color: red;"&gt;'+ e.entries[i].dataPoint.y + '&lt;/span&gt; &lt;br/&gt;' ; str = str.concat(temp); } return (str); },
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多