代码之家  ›  专栏  ›  技术社区  ›  runfaj

面积样条线:边到边线并隐藏第一个y标签

  •  0
  • runfaj  · 技术社区  · 9 年前

    我收到了一个使用areaspline的图形模型(忽略柱状图):

    graph

    1. 有没有一种方法可以根据它们的趋势斜率,或者可能只是稍微高于或低于端点,将这些线延伸到图形的边缘?在这样的图上,省略这些边线看起来不错,但在同一区域中只有3个数据点的图上,空白更为明显。
    2. 我让y轴标签彼此步进,但在这种情况下,我似乎找不到隐藏第一个标签(0美元)的方法。除了隐藏文本元素本身之外,还有这样的配置吗?

    编辑

    var themeColors = {
        primaryColor: '#067bc2',    //dark blue
        secondaryColor: '#6a61a7',  //dark purple
        tertiaryColor: '#5ccadc',   //turquoise
        darkGray: '#37383e',        //dark gray
        mediumGray: '#919aa2',      //medium gray
        mediumLightGray: '#c9d3dc', //medium blue gray
        lightGray: '#eaf1f8',       //light blue gray (hover color)
        primaryRed: '#e54443',
        secondaryRed: '#e54443',
        primaryGreen: '#2bbb2d',
        secondaryGreen: '#2bbb2d',
        primaryOrange: '#ffa883',   //peach
        primaryBackground: '#fafcfe',//light blue
        secondaryBackground: '#ffffff',
        primaryText: '#37383e',     //dark gray
        secondaryText: '#919aa2',   //medium gray
        selectedColor: '#f5fafd'    //light blue, slightly darker than background
    };
    
    var colors = [
      themeColors.secondaryColor,
      themeColors.tertiaryColor,
      themeColors.primaryOrange
    ];
    
    (function renderChart(series) {
      var options = {
        chart: {
          height: 400,
          spacing: [0,0,0,0],
          type: 'areaspline',
        },
        colors: colors,
        credits: { enabled: false },
        legend: {
          align: 'left',
          itemStyle: {
            color: themeColors.secondaryText,
            fontSize: '12px',
            fontWeight: 'normal',
          },
          margin: 30,
          symbolHeight: 14,
          symbolRadius: 0,
          symbolWidth: 14,
        },
        plotOptions: {
          areaspline: {
            marker: {
              fillColor: themeColors.secondaryBackground,
              lineColor: null,
              lineWidth: 2,
              radius: 3,
              symbol: 'circle',
            }
          }
        },
        title: { text: null },
        xAxis: {
          categories: ['first', 'second', 'third'],
          crosshair: {
            color: Highcharts.color(themeColors.lightGray).setOpacity(.5).get('rgba'),
            width: 12
          },
          labels: {
            style: {
              color: themeColors.secondaryText,
              fontSize: '12px'
            }
          },
          lineColor: themeColors.lightGray,
          tickColor: themeColors.lightGray,
          tickWidth: 0,
        },
        yAxis: {
          gridLineColor: themeColors.lightGray,
          labels: {
            align: 'left',
            step: 2,
            style: {
              color: themeColors.secondaryText,
              fontSize: '12px'
            },
            x: 0,
            y: 12,
          },
          lineColor: themeColors.lightGray,
          tickColor: themeColors.lightGray,
          title: { text: null },
        },
      };
    
      series.forEach((s,i)=>{
        s.fillColor = {
          linearGradient: {
            x1: 0,
            y1: 0,
            x2: 0,
            y2: 1
          },
          stops: [
            [0, Highcharts.color(colors[i]).setOpacity(0.1).get('rgba')],
            [1, Highcharts.color(colors[i]).setOpacity(0).get('rgba')]
          ]
        };
      });
    
      options.series = series;
    
        Highcharts.chart('container',options);
    })([
        { name: 'a', data:[1,4,2] },
      { name: 'b', data:[3,1,2] }
    ]);
    

    JSFIDLE: https://jsfiddle.net/mtowj6ng/

    谢谢

    1 回复  |  直到 9 年前
        1
  •  1
  •   morganfree    9 年前
    1. 可以设置轴的最小值和最大值。

      xAxis: {
        categories: ['first', 'second', 'third'],
        min: 0.5,
        max: 1.5,
      

    如果有3个类别,轴范围将为0-2,因此可以将最小值增加,将最大值减少0.5(类别的中间)。

    您还可以通过使用更新xAxis来动态完成此操作 xAxis.update 在图表上加载。

        chart: {
          events: {
            load: function () {
              const min = Math.min.apply(null, this.series[0].xData)
              const max = Math.max.apply(null, this.series[0].xData)
    
              this.xAxis[0].update({
                min: min + 0.5,
                max: max - 0.5
              })
            }
          }
        },
    

    也可以在渲染图表之前执行此操作。

    1. 设置 yAxis.showFirstLabel 为false。

    例子: https://jsfiddle.net/bbd3pu9j/

    推荐文章