我正在尝试创建一个与时间相关的图表。我想给每个子弹以年,月,日,小时,分钟和秒。但是,如果用户放大,我希望X轴不显示分钟和秒。

我的问题:必须为chart.dataDateFormat和数据的“date”参数指定日期格式?

到目前为止,这是我的代码。请让我知道我应该更改的内容。
谢谢。

<script type="text/javascript">
            var chart;

            var chartData = [
                {
                    "date": "2012-01-01",
                    "value": 0.24
                },
                {
                    "date": "2012-01-02",
                    "value": 0.28
                },
                {
                    "date": "2012-01-03",
                    "value": 0.34
                },
                {
                    "date": "2012-01-04",
                    "value": 0.30
                },
                {
                    "date": "2012-01-05",
                    "value": 0.27
                }
            ];


            AmCharts.ready(function () {
                // SERIAL CHART
                chart = new AmCharts.AmSerialChart();
                chart.pathToImages = "/images/";
                chart.dataProvider = chartData;
                chart.dataDateFormat = "YYYY-MM-DD";
                chart.categoryField = "date";


                // AXES
                // category
                var categoryAxis = chart.categoryAxis;
                categoryAxis.parseDates = true; // as our data is date-based, we set parseDates to true
                categoryAxis.minPeriod = "hh"; // our data is daily, so we set minPeriod to DD
                categoryAxis.gridAlpha = 0.1;
                categoryAxis.minorGridAlpha = 0.1;
                categoryAxis.axisAlpha = 0;
                categoryAxis.minorGridEnabled = true;
                categoryAxis.inside = true;

                // value
                var valueAxis = new AmCharts.ValueAxis();
                valueAxis.tickLength = 0;
                valueAxis.axisAlpha = 0;
                valueAxis.showFirstLabel = false;
                valueAxis.showLastLabel = false;
                chart.addValueAxis(valueAxis);

                // GRAPH
                var graph = new AmCharts.AmGraph();
                graph.dashLength = 3;
                graph.lineColor = "gray";
                graph.valueField = "value";

                graph.dashLength = 3;
                graph.bullet = "round";
                graph.lineThickness = 0;
                graph.balloonText = "[[category]]<br><b><span style='font-size:14px;'>value:[[value]]</span></b>";
                chart.addGraph(graph);

                // CURSOR
                var chartCursor = new AmCharts.ChartCursor();
                chart.addChartCursor(chartCursor);

                // SCROLLBAR
                var chartScrollbar = new AmCharts.ChartScrollbar();
                chart.addChartScrollbar(chartScrollbar);

                // HORIZONTAL GREEN RANGE
                var guide = new AmCharts.Guide();
                guide.value = 0.26;
                guide.toValue = 0.32;
                guide.fillColor = "#00CC00";
                guide.inside = true;
                guide.fillAlpha = 0.2;
                guide.lineAlpha = 0;
                valueAxis.addGuide(guide);

                // TREND LINES
                // first trend line
                var trendLine = new AmCharts.TrendLine();
                // note,when creating date objects 0 month is January, as months are zero based in JavaScript.
                trendLine.initialDate = new Date(2012, 0, 1, 0,59); // 12 is hour - to start trend line in the middle of the day
                trendLine.finalDate = new Date(2012, 0, 23, 24);
                trendLine.initialValue = 0.28;
                trendLine.finalValue = 0.28;
                trendLine.lineColor = "#CC0000";
                chart.addTrendLine(trendLine);

                // WRITE
                chart.write("chartdiv");
            });
    </script>

最佳答案

我终于在一夜之间想通了。我将解决方案提供给想要的人。

chartData必须为:



chart.dataDateFormat必须为:



希望它能帮助某人。

09-25 16:58