简体   繁体   English

如何将自定义 CSS 添加到<a>带有链接的 highcharts.js 锚标签</a>

[英]How to add custom CSS to the highcharts.js anchor <a> tags with links

I am attempting to an tag on the names of my categories to re-direct to a different page if the title is clicked on.如果单击标题,我正在尝试在我的类别名称上添加标签以重定向到不同的页面。 At the moment my css class names aren't applying any styling.目前我的 css class 名称没有应用任何样式。 When inspecting element I see that the title I wrapped around in the tag has a class of "highcharts-anchor" I'm attempting to either override highcharts-anchor, or add custom css to it to display the look that I would like.检查元素时,我看到我在标签中包裹的标题有一个“highcharts-anchor”的 class 我试图覆盖 highcharts-anchor,或者添加自定义 css 以显示我想要的外观。 I've attempted to add a color: red to highcharts-anchor but it has no effect to my graph.我试图向 highcharts-anchor 添加一种颜色:红色,但它对我的图表没有影响。

Also in my second title, the anchor tag is only applied to the first line of text, I am attempting to have the entire 3 rows of text in "2. Most Sold Online Stores Using Facebook Advertisements" to be clickable, at the moment "Most Sold Online Stores Using" is the only text that is linking to another page.同样在我的第二个标题中,锚标记仅应用于第一行文本,我正在尝试让“2. 使用 Facebook 广告的最畅销在线商店”中的整个 3 行文本可点击,此刻“大多数销售的在线商店使用“是链接到另一个页面的唯一文本。

Here is a code snippet of what I am attempting to do:这是我正在尝试做的代码片段:

      xAxis: {
        categories: ['<a class="highcharts-anchor" href="google.com"> 1. Highest Sales. </a>', '2. <a class="anchor-text" href="google.com"> Most Sold Online Stores Using Facebook Advertisements </a>'],
        labels: {
          align: 'left',
          x: -180,
        }
      },


.anchor-text {
  color: red;
  font-size: 20px;
  font-weight: bold;
}

.highcharts-anchor {
  color: red;
}

Here is a link to my jsfiddle https://jsfiddle.net/bs1o2yth/10/这是我的 jsfiddle https://jsfiddle.net/bs1o2yth/10/的链接

You can simply use formatter to color and add anchor tags to your labels.您可以简单地使用格式化程序为标签color和添加anchor标记。

formatter function return the value of labels and other data.格式化程序function 返回标签值和其他数据。 Just check which pos you want to color with and add your relevant anchor tag.只需检查您想用哪个pos着色并添加相关的anchor标记。 pos will from 0 to 4 in your case as you have five labels only.在您的情况下, pos将从04 ,因为您只有five标签

I have recreated your example and added anchor tags to all of the labels which you can click on and visit the href link.我重新创建了您的示例并将anchor标记添加到您可以click并访问href链接的所有标签。

Run snippet below to see it working.运行下面的代码片段以查看它的工作原理。

 Highcharts.chart('container', { chart: { type: 'bar', marginLeft: 200 }, xAxis: { categories: ['1. Highest Sales.', '2. Sed pretium aliquet dapibus.', '3. Donec mollis sit amet elit.', '4. Donec mollis sit amet elit.', '5. Donec mollis sit amet elit.'], labels: { align: 'left', x: -180, formatter: function() { if (this.pos == 0) { return '<a href="https://google.com" style="fill: green;">' + this.value + '</a>'; } else if (this.pos == 1) { return '<a href="https://google.com" style="fill: #FF00FF;">' + this.value + '</a>'; } else if (this.pos == 2) { return '<a href="https://google.com" style="fill: blue;">' + this.value + '</a>'; } else if (this.pos == 3) { return '<a href="https://google.com" style="fill: yellow;">' + this.value + '</a>'; } else if (this.pos == 4) { return '<a href="https://google.com" style="fill: green;">' + this.value + '</a>'; } else { return this.value } } } }, plotOptions: { series: { dataLabels: { enabled: true, format: '{y} %' } } }, series: [{ data: [{ y: 5, color: "#00FF00" }, { y: 10, color: "#FF00FF" }, { y: 7, color: "blue" }, { y: 3, color: "yellow" }, { y: 7, color: "blue" }] }] });
 <script src="https://code.highcharts.com/highcharts.js"></script> <div class='col-md-6'> <div id="container"></div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM