如何防止在具有时间刻度的图表中切断第一个/最后一个条形

gle*_*-84 14 momentjs chart.js

如何防止第一个和最后一个条被切断(显示一半)?

我需要在x轴上显示短月份名称.我尝试过使用各种最小/最大设置,但我似乎无法正确使用它.

例

var graphData = {
  dates: [
    '2016-06-01',
    '2016-07-01',
    '2016-08-01',
    '2016-09-01',
    '2016-10-01',
    '2016-11-01',
    '2016-12-01',
    '2017-01-01',
    '2017-02-01',
    '2017-03-01',
    '2017-04-01',
    '2017-05-01'
  ],
  wins: [23, 5, 13, 24, 8, 11, 23, 5, 13, 24, 8, 11],
  draws: [2, 1, 2, 0, 2, 2, 3, 1, 2, 4, 0, 1],
  losses: [3, 1, 2, 10, 8, 8, 3, 1, 2, 10, 8, 8],
  winRates: [50, 40, 72, 30, 46, 80, 50, 40, 72, 30, 46, 80]
};

var winsMax = Math.max.apply(Math, graphData.wins);
var lossesMax = Math.max.apply(Math, graphData.losses);

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
  type: "bar",
  data: {
    labels: graphData.dates.map((date) => moment(date)),
    datasets: [
      {
        type: "bar",
        backgroundColor: "green",
        hoverBackgroundColor: "green",
        data: graphData.wins,
        yAxisID: "winsAndLosses"
      },
      {
        type: "bar",
        backgroundColor: "red",
        hoverBackgroundColor: "red",
        data: graphData.losses.map((i) => -i),
        yAxisID: "winsAndLosses"
      },
      {
        type: "line",
        data: graphData.winRates,
        fill: true,
        backgroundColor: "gray",
        pointRadius: 0,
        pointHoverRadius: 0,
        yAxisID: "winRate"
      }
    ]
  },
  options: {
    legend: {
      display: false
    },
    scales: {
      xAxes: [{
        type: "time",
        time: {
          unit: "month",
          displayFormats: {
            month: "MMM"
          }
        },
        stacked: true,
        gridLines: {
          display: false
        },
        ticks: {
          callback: (label) => label.toUpperCase(),
          fontSize: 10     
        }
      }],
      yAxes: [
        {
          id: "winsAndLosses",
          stacked: true,
          ticks: {
            min: (lossesMax + 10) * -1,
            max: winsMax + 10,
            callback: (label) => Math.abs(label) // TODO: Localization (number formatting).
          },
          display: false
        },
        {
          id: "winRate",
          ticks: {
            min: 0,
            max: 100,
            stepSize: 10,
            callback: (label) => label + "%", // TODO: Localization (number formatting).
            fontSize: 10
          }
        }
      ]
    }
}
});
Run Code Online (Sandbox Code Playgroud)
.myChartDiv {
  max-width: 800px;
  max-height: 400px;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://npmcdn.com/chart.js@latest/dist/Chart.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<html>
  <body>
    <div class="myChartDiv">
      <canvas id="myChart" width="800" height="400"></canvas>
    </div>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

AMT*_*AMT 26

有一个名为offset的设置似乎对我有用:

xAxes: [{
     offset: true
  }]
Run Code Online (Sandbox Code Playgroud)

var graphData = {
  dates: [
    '2016-06-01',
    '2016-07-01',
    '2016-08-01',
    '2016-09-01',
    '2016-10-01',
    '2016-11-01',
    '2016-12-01',
    '2017-01-01',
    '2017-02-01',
    '2017-03-01',
    '2017-04-01',
    '2017-05-01'
  ],
  wins: [23, 5, 13, 24, 8, 11, 23, 5, 13, 24, 8, 11],
  draws: [2, 1, 2, 0, 2, 2, 3, 1, 2, 4, 0, 1],
  losses: [3, 1, 2, 10, 8, 8, 3, 1, 2, 10, 8, 8],
  winRates: [50, 40, 72, 30, 46, 80, 50, 40, 72, 30, 46, 80]
};

var winsMax = Math.max.apply(Math, graphData.wins);
var lossesMax = Math.max.apply(Math, graphData.losses);

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
  type: "bar",
  data: {
    labels: graphData.dates.map((date) => moment(date)),
    datasets: [
      {
        type: "bar",
        backgroundColor: "green",
        hoverBackgroundColor: "green",
        data: graphData.wins,
        yAxisID: "winsAndLosses"
      },
      {
        type: "bar",
        backgroundColor: "red",
        hoverBackgroundColor: "red",
        data: graphData.losses.map((i) => -i),
        yAxisID: "winsAndLosses"
      },
      {
        type: "line",
        data: graphData.winRates,
        fill: true,
        backgroundColor: "gray",
        pointRadius: 0,
        pointHoverRadius: 0,
        yAxisID: "winRate"
      }
    ]
  },
  options: {
    legend: {
      display: false
    },
    scales: {
      xAxes: [{
        type: "time",
        time: {
          unit: "month",
          displayFormats: {
            month: "MMM"
          }
        },
        stacked: true,
        gridLines: {
          display: false
        },
        ticks: {
          callback: (label) => label.toUpperCase(),
          fontSize: 10     
        },
        offset:true
      }],
      yAxes: [
        {
          id: "winsAndLosses",
          stacked: true,
          ticks: {
            min: (lossesMax + 10) * -1,
            max: winsMax + 10,
            callback: (label) => Math.abs(label) // TODO: Localization (number formatting).
          },
          display: false
        },
        {
          id: "winRate",
          ticks: {
            min: 0,
            max: 100,
            stepSize: 10,
            callback: (label) => label + "%", // TODO: Localization (number formatting).
            fontSize: 10
          }
        }
      ]
    }
}
});
Run Code Online (Sandbox Code Playgroud)
.myChartDiv {
  max-width: 800px;
  max-height: 400px;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://npmcdn.com/chart.js@latest/dist/Chart.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<html>
  <body>
    <div class="myChartDiv">
      <canvas id="myChart" width="800" height="400"></canvas>
    </div>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

  • 让我的一天!✌️ (7认同)
  • 你是对的。试试 `xAxes: [{offset: true}]` 可以在这里找到:http://www.chartjs.org/docs/latest/axes/cartesian/ 在第一个图表中 (2认同)
  • 谢谢,偏移量肯定有效,但对我来说,它添加了过大的填充,看起来很糟糕。我找不到任何关于如何配置其大小的方法。我最终将虚拟(空)数据添加到数据集的最后位置,解决了这个问题。我的图表包含数百个条形图,如果您只有几个条形图,这可能无法使用 (2认同)