如何在Highcharts仪表中完成响应式文本?

时间:2016-08-30 13:14:02

标签: javascript css highcharts responsive

我用Highcharts构建了一个速度表图表,结合了规格和饼图类型。在图表中,我有不同大小的文本,针对250x250像素大小进行了优化。

我想要完成的是在调整图表大小时保持字体大小/图形容器比率。无论容器的大小如何,基本上都具有相同的外观。

我现在在包含图表的div上有一个基于像素的字体大小(通常是我项目的基本字体大小),在图表本身上有1em(.highcharts-data-labels)。里面是我的自定义文本,标有css类(.gauge-value,.gauge-text,.gauge-unit),我试图给出合理的em值。

实现这一目标的最佳方法是什么?

JSFiddle with demo

JS

$(function() {
  var settings = {
    gaugeMinValue: 0,
    gaugeMaxValue: 8000,
    gaugeStartValue: 3000,
    gaugeStartAngle: -160,
    gaugeEndAngle: 160,
    gaugeUpdateInterval: 500 // ms
  };

  $('#gauge1').highcharts({
    tooltip: {
      enabled: false
    },
    chart: {
      type: 'gauge',
      backgroundColor: 'rgba(255, 255, 255, 0)',
      plotBackgroundColor: null,
      plotBackgroundImage: null,
      plotBorderWidth: 0,
      plotShadow: false,
      spacing: [5, 30, 5, 30],
      style: {
        fontSize: '1em'
      }
    },

    title: false,

    pane: {
      startAngle: settings.gaugeStartAngle,
      endAngle: settings.gaugeEndAngle
    },

    plotOptions: {
      gauge: {
        dial: {
          radius: 0
        },
        pivot: {
          radius: 0
        },
        dataLabels: {
          borderWidth: 0,
          padding: 0,
          verticalAlign: 'middle',
          style: false,
          formatter: function() {
            var output = '<div class="gauge-data">';
            output += '<span class="gauge-value">' + this.y + '</span>';
            output += '<span class="gauge-text">Engine LOAD</span>';
            output += '<span class="gauge-unit">KW</span>';
            output += '</div>';

            return output;
          },
          useHTML: true
        }
      },
      pie: {
        dataLabels: {
          enabled: true,
          distance: -10,
          style: false
        },
        startAngle: settings.gaugeStartAngle,
        endAngle: settings.gaugeEndAngle,
        center: ['50%', '50%'],
        states: {
          hover: {
            enabled: false
          }
        }
      }
    },

    // the value axis
    yAxis: {
      offset: 0,
      min: settings.gaugeMinValue,
      max: settings.gaugeMaxValue,

      title: false,

      minorTickWidth: 0,

      tickPixelInterval: 30,
      tickWidth: 2,
      tickPosition: 'outside',
      tickLength: 14,
      tickColor: '#ccc',
      lineColor: '#ccc',
      labels: {
        distance: 28,
        rotation: "0",
        step: 2,
      },

      plotBands: [{
        thickness: 10,
        outerRadius: "112%",
        from: 0,
        to: 2500,
        color: '#FB8585' // red
      }, {
        thickness: 10,
        outerRadius: "112%",
        from: 2500,
        to: 5500,
        color: '#F9E7AE' // yellow,
      }, {
        thickness: 10,
        outerRadius: "112%",
        from: 5500,
        to: 8000,
        color: '#83DAD9' // green
      }]
    },

    series: [{
      type: 'gauge',
      data: [settings.gaugeStartValue],
    }, {
      type: 'pie',
      innerSize: '87%',
      data: [{
        y: settings.gaugeStartValue,
        name: "",
        color: "#0bbeba"
      }, {
        y: settings.gaugeMaxValue - settings.gaugeStartValue,
        name: '',
        color: "#666666"
      }]
    }],

    navigation: {
      buttonOptions: {
        enabled: false
      }
    },

    credits: false
  });
});

CSS

.container {
  width: 50%;
  margin: 0 auto;
  font-size: 16px;
}

.gauge {
  width: 100%;
  max-width: 350px;
  max-height: 350px;
  min-width: 250px;
  min-height: 250px;
  padding: 0;
  border: 1px solid #666;
  background: #F8F8F8;
}

.gauge-data {
  text-align: center;
  color: #666;
  display: block;
}

.gauge-data > * {
  display: block;
}

.gauge-value {
  font-size: 3em;
}

.gauge-text {
  font-size: 1.0em;
  font-weigt: normal;
  margin-top: 10px;
}

.gauge-unit {
  margin-top: 5px;
  font-size: .9em;
}

图片1:这是最佳视图

Optimized

图片2:随着容器尺寸的增加,字体大小不会与图表一起缩放

Font size is not scaling along with the graph

2 个答案:

答案 0 :(得分:2)

您可以更新量表系列的dataLabel,并使用内联CSS根据当前图表宽度为每个文本行设置字体大小。

演示:https://jsfiddle.net/sxkz9q32/

代码的相关部分:

  var options = {
    tooltip: {
      enabled: false
    },
    chart: {
      type: 'gauge',
      backgroundColor: 'rgba(255, 255, 255, 0)',
      plotBackgroundColor: null,
      plotBackgroundImage: null,
      plotBorderWidth: 0,
      plotShadow: false,
      spacing: [5, 30, 5, 30],
      style: {
        fontSize: '1em'
      },
      events: {
        redraw: function() {
          var chart = this;
          if (chart.lastWidth !== chart.plotWidth) {
            chart.lastWidth = chart.plotWidth;
            chart.series[0].update({ //apply changes to center the text
              dataLabels: {
                formatter: function() {
                  var output = '<div class="gauge-data">';
                  output += '<span class="gauge-value" style="font-size: ' + chart.plotWidth / 66 + 'em;">' + this.y + '</span>';
                  output += '<span class="gauge-text" style="font-size: ' + chart.plotWidth / 190 + 'em;">Engine LOAD</span>';
                  output += '<span class="gauge-unit" style="font-size: ' + chart.plotWidth / 211 + 'em;">KW</span>';
                  output += '</div>';

                  return output;
                }
              }
            }); 
          }
        }
      }
    },

我使用了基于宽度的值,对我来说很合适,但可以使用自己的计算。

答案 1 :(得分:1)

只要您的图形按屏幕尺寸缩放

,就可以使用vw
.gauge-value {
  font-size: 8vw;
}