固定X轴刻度,从不更改弹幕

时间:2019-06-27 04:43:37

标签: javascript amcharts

enter image description here

如果可以在X轴上看到 有时间增量6小时 现在,我想将其刻度更改为2小时

我昨天才刚刚学习这枚安乐果,真的对如何使用它了解不多。

这是代码

<script type="text/javascript">
//am4core.useTheme(am4themes_kelly);

am4core.useTheme(am4themes_animated);
// Themes end


// Create chart
var chart = am4core.create("chartdiv", am4charts.XYChart);
chart.paddingRight = 20;

chart.data = generateChartData();

var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.baseInterval = {
  "timeUnit": "minute",
  "count": 1
};
dateAxis.tooltipDateFormat = "HH:mm, d MMMM";

var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.tooltip.disabled = true;
valueAxis.title.text = "Fin 1";

var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.dateX = "date";
series.dataFields.valueY = "visits";
series.tooltipText = "Stacker: [bold]{valueY}[/]";
series.fillOpacity = 0.3;

chart.cursor = new am4charts.XYCursor();
chart.cursor.lineY.opacity = 0;
chart.scrollbarX = new am4charts.XYChartScrollbar();
chart.scrollbarX.series.push(series);


chart.events.on("datavalidated", function () {
    dateAxis.zoom({start:0, end:1});
});


function generateChartData() {
    var chartData = [];
    // current date
    var firstDate = new Date();
    // now set 12:00 am
    firstDate.setHours(0,0,0,0);

    // and generate 500 data items
    var visits = 0;
    for (var i = 0; i < 1442; i++) {
        var newDate = new Date(firstDate);
        // each time we add one minute
        newDate.setMinutes(newDate.getMinutes() + i);

        chartData.push({
        date: newDate,
        visits: visits
        });

    }
    return chartData;
}
</script>

我已经编辑了代码,可以在此处进行编辑: https://codepen.io/muhammad-syazani/pen/wLrbQE

1 个答案:

答案 0 :(得分:1)

是的,使用日期轴可能会很棘手。即使您使用amCharts v4已有一段时间,也可能不清楚如何以所需方式驯服该轴。我强烈建议您完整阅读our guide on the Date Axis,这确实有帮助。图表基本上会尝试找出如何将频率很好地适合图表。

您要调整的第一件事是dateAxis.renderer.minGridDistance。默认情况下,其120像素。样本图表有24小时,每2小时的最小像素间隔为120px,您的chart.plotContainer区域必须为120 * (24/2),或者至少为1440px,浏览器窗口必须更宽比起那个来说。因此,如果我们将其削减,我们更有可能能够适应2小时间隔的网格。

接下来的东西(这是主要成分)是dateAxis.gridIntervals。如果您查看生成的文档,则会看到图表将选择的间隔列表。每小时一次,或者每3个小时一次,但不是每2个小时一次。如果我们将列表替换为间隔2小时的列表,如果可以的话,图表将使用该列表。

您也希望在scrollBarX的日期轴上执行相同的操作。

相关代码:

dateAxis.renderer.minGridDistance = 75; // 75 * 24/2 == 900 minimum plotContainer width requisite

var gridIntervals = [
 { timeUnit: "millisecond", count: 1 },
 { timeUnit: "millisecond", count: 5 },
 { timeUnit: "millisecond", count: 10 },
 { timeUnit: "millisecond", count: 50 },
 { timeUnit: "millisecond", count: 100 },
 { timeUnit: "millisecond", count: 500 },
 { timeUnit: "second", count: 1 },
 { timeUnit: "second", count: 5 },
 { timeUnit: "second", count: 10 },
 { timeUnit: "second", count: 30 },
 { timeUnit: "minute", count: 1 },
 { timeUnit: "minute", count: 5 },
 { timeUnit: "minute", count: 10 },
 { timeUnit: "minute", count: 30 },
 { timeUnit: "hour", count: 1 },
 { timeUnit: "hour", count: 2 }, // This is the 2-hour interval
 { timeUnit: "hour", count: 3 },
 { timeUnit: "hour", count: 6 },
 { timeUnit: "hour", count: 12 },
 { timeUnit: "day", count: 1 },
 { timeUnit: "day", count: 2 },
 { timeUnit: "day", count: 3 },
 { timeUnit: "day", count: 4 },
 { timeUnit: "day", count: 5 },
 { timeUnit: "week", count: 1 },
 { timeUnit: "month", count: 1 },
 { timeUnit: "month", count: 2 },
 { timeUnit: "month", count: 3 },
 { timeUnit: "month", count: 6 },
 { timeUnit: "year", count: 1 },
 { timeUnit: "year", count: 2 },
 { timeUnit: "year", count: 5 },
 { timeUnit: "year", count: 10 },
 { timeUnit: "year", count: 50 },
 { timeUnit: "year", count: 100 }
];
dateAxis.gridIntervals.setAll(gridIntervals);

chart.scrollbarX.events.on("validated", function() {
  chart.scrollbarX.scrollbarChart.xAxes.getIndex(0).gridIntervals.setAll(gridIntervals);
});

叉子:

https://codepen.io/team/amcharts/pen/9ba62a5bb5239fee149c2d46bf1e4b81

希望这会有所帮助!

(顺便说一句-萨拉姆斯!我喜欢用户名,那是童话尾巴的引用吗?)