简体   繁体   English

Chartjs 和(更紧凑的)标题放置

[英]Chartjs and (more compact) title placement

I am using Chartjs for some simple gauges (using pie chart) but when adding the title it is quite far apart and padding doesn't seem to help me:-( Does anyone know a way of placing the title closer to the gauge/pie chart?我将 Chartjs 用于一些简单的仪表(使用饼图)但是当添加标题时它相距很远并且填充似乎对我没有帮助:-(有谁知道将标题放置在靠近仪表/饼图的方法图表?
image of title in pie/gauge chart here饼图/仪表图中的标题图片在这里

var ctx103 = document.getElementById('chart103').getContext('2d');
new Chart(ctx103, {
  type: 'doughnut',
  data: {
    datasets: [{
      //label: '# of Votes',
      data: [80, 20],
      backgroundColor: ["green", "grey"]
    }]
  },
  options: {
    rotation: 270, // start angle in degrees
    circumference: 180, // sweep angle in degrees
    plugins: {
      title: {
        display: true,
        position: 'top',
        text: 'TITLE !!!!!',
        padding: {
          top: 0,
          bottom: 0
        }
      }
    }
  }
});

I have been trying different padding options but that just moves the title further away...我一直在尝试不同的填充选项,但这只会将标题移得更远......

While this may not work for your specific application, in some cases this can be achieved using HTML/CSS.虽然这可能不适用于您的特定应用程序,但在某些情况下可以使用 HTML/CSS 实现。

This is the styling I used:这是我使用的样式:

<style>
  #titleAndChart {
    margin: 0;
    position: absolute;
    height: 600px;
    width: 600px;
    box-sizing: border-box;
  }
  #titleDiv {
    position: absolute;
    top: 0px;
    left: 0px;
    width: 600px;
  }
  #chartDiv {
    position: absolute;
    top: 60px;
    left: 0px;
    width: 600px;
  }
</style>

The HTML layout then becomes: HTML 布局变为:

  <div id="titleAndChart">
    <div id="titleDiv">
      Chart title goes here
    </div> 
    <div id='chartDiv'>
      <canvas id="myChart"></canvas>
    </div>
  </div>

And the chart itself looks like this:图表本身看起来像这样:

Chart.defaults.doughnut.circumference = Math.PI;
Chart.defaults.doughnut.rotation = -1 * Math.PI;
new Chart(document.getElementById('myChart'), {
  type: 'doughnut',
  options: {
    legend: {
      display: false,
    }
  },
  data: {
    labels: ['Red','Green'],
    datasets: [{
      data: [25,75],
      backgroundColor: ['red','green']
    }]
  }
});

Here's a fiddle using ChartJS v2.9.4:这是使用 ChartJS v2.9.4 的小提琴:

https://jsfiddle.net/hdahle/4x670et8/ https://jsfiddle.net/hdahle/4x670et8/

And here's a fiddle using ChartJS v4.0.1这是使用 ChartJS v4.0.1 的小提琴

https://jsfiddle.net/hdahle/k8zxvjyq/ https://jsfiddle.net/hdahle/k8zxvjyq/

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

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