Amcharts Inherit Font or set all Element Font

你说的曾经没有我的故事 提交于 2020-01-15 10:43:26

问题


I've set my page css to:

html { 
    font-family: 'Not the Default AmCharts Font';
}

I haven't specified a font in my chart code, but the charts are not inheriting the page's css defined font.

Is there a way to globally set the font for all Amcharts elements in one go? Something like:

AmCharts.defaultFontFamily = 'Not the Default AmCharts Font';

I'd also be happy with some sort of jQuery solution like:

$('.amcharts').find('*').css('font-family', 'Not the Default AmCharts Font');

Neither of these solutions are working.

I'd really like this to impact at least the following: Chart Title , Axes Titles , Axes Labels , Graph Labels , Balloon Text , Legend Labels , Legend Values


回答1:


Since AmCharts uses inline styles and attributes, you'll likely need to use !important to override the fonts

.yourchartclass * {
  font-family: 'Impact' !important;
}

Another way to go about this is to leverage the AmCharts.addInitHandler method to create a global init listener that runs on as many types of charts as you want and sets the fontFamily and other properties during initialization. For example:

AmCharts.addInitHandler(function(chart) {
  chart.fontFamily = 'Oswald';
  chart.fontSize = 16;
});

You can also specify an array of chart types to limit what the initHandler can run on. You can also add custom flags to your chart object and use them to determine which settings you want to set, for example:

AmCharts.addInitHandler(function(chart) {
  if (chart.useLato) {
    chart.fontFamily = "Lato";
    chart.fontSize = 16;
  } else if (chart.useRoboto) {
    chart.fontFamily = "Roboto";
    chart.fontSize = 12;
  } else {
    chart.fontFamily = "Oswald";
    chart.fontSize = 12;
  }
});

Here's a demo of this in action.



来源:https://stackoverflow.com/questions/45923814/amcharts-inherit-font-or-set-all-element-font

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!