不显示MSChart x轴标签

本文关键字:标签 MSChart 显示 | 更新日期: 2023-09-27 18:18:23

我目前正在构建一个应用程序,显示一个线形图,并使用MSChartControl。

相关代码如下:

WeatherDatabase _db = new WeatherDatabase(Properties.Settings.Default.SqlConnectionString);
    private void AddSensorDataToGraph(int sensorType, string xTitle, string yTitle, string Title)
    {
        var sensorIdList = (from d in _db.Sensors where d.TypeId == sensorType select d.Id).ToArray();
        var startDate = dateTimePickerStart.Value;
        var stopDate = dateTimePickerStop.Value;
        SetMaxMinValues(dateTimePickerStart.Value.ToOADate(), dateTimePickerStop.Value.ToOADate());
        this.Text = chartValues.Titles["Titel"].Text = String.Format("{0} vom {1} bis {2}", Title, startDate, stopDate);
        chartValues.ChartAreas[CHARTAREA].AxisX.Title = xTitle;
        chartValues.ChartAreas[CHARTAREA].AxisY.Title = yTitle;
        // Alte Datensätze löschen
        chartValues.Series.Clear();
        foreach (var sensorId in sensorIdList)
        {
            var values = (from d in _db.DeviceIO where d.IdSensor == sensorId && d.Timestamp > startDate && d.Timestamp < stopDate orderby d.Timestamp ascending select d).ToArray();
            if (values.Length > 0)
            {
                var desc = values[0].Sensors.SensorType.Description;
                chartValues.Series.Add(GenerateDefaultSeries(String.Format("{0}'r'nSensorId: {1}", desc, sensorId)));
                //chartValues.Series.Add("Test" + sensorId);
                int seriesId = chartValues.Series.Count - 1;
                foreach (var item in values)
                {
                    if (item.Value == null)
                    {
                        continue;
                    }
                    if ((double)item.Value == NOVALUE)
                    {
                        continue;
                    }
                    AddPointToChart((DateTime)item.Timestamp, (double)item.Value, seriesId);
                }
            }
        }
    }
    private void AddPointToChart(DateTime timestamp, double value, int series)
    {
        DataPoint dataPoint = chartValues.Series[series].Points.Add(value);
        dataPoint.XValue = timestamp.ToOADate();
        dataPoint.AxisLabel = "dd.MM.yy hh:mm";
    }
    private void SetMaxMinValues(double min, double max)
    {
        if (min < max)
        {
            chartValues.ChartAreas[CHARTAREA].AxisX.Minimum = min;
            chartValues.ChartAreas[CHARTAREA].AxisX.Maximum = max;
        }
    }
    private Series GenerateDefaultSeries(string Name)
    {
        // Grundeinstellungen für die Data-Series setzen
        Series series = new Series();
        series.ChartType = SeriesChartType.Line;
        series.Name = Name;
        series.IsValueShownAsLabel = false;
        series.IsVisibleInLegend = true;
        series.IsXValueIndexed = false;
        series.AxisLabel = "dd.MM.yy hh:mm";
        // series.LabelFormat = "g";
        series.XAxisType = AxisType.Primary;
        series.XValueType = ChartValueType.Auto;
        series.YValuesPerPoint = 1;
        //    series.AxisLabel = "dd.MM.yy hh:mm";
        return series;
    }

如果在设计器模式下添加一个系列,则x轴标签正确点击查看图片
但如果我启动我的应用程序并以编程方式添加一个系列它看起来像这样

有人知道为什么x轴标题/标签没有正确显示吗?

编辑:基于Tom的帮助,我改变了一些东西:

var seriesName = String.Format("{0}'r'nSensorId: {1}", desc, sensorId);
                var curSeries = chartValues.Series.Add(seriesName);
                UpdateSeriesSettings(ref curSeries);
              [...]
     AddPointToChart((DateTime)item.Timestamp, (double)item.Value, curSeries);

[…]

private void UpdateSeriesSettings(ref Series series)
{
    // Grundeinstellungen für die Data-Series setzen
    // Series series = new Series();
    series.ChartType = SeriesChartType.Spline;
    series.IsValueShownAsLabel = false;
    series.IsVisibleInLegend = true;
    series.IsXValueIndexed = false;
    series.AxisLabel = "dd.MM.yy hh:mm";
    // series.LabelFormat = "g";
    series.XAxisType = AxisType.Primary;
    series.XValueType = ChartValueType.Auto;
    series.YValuesPerPoint = 1;
    //    series.AxisLabel = "dd.MM.yy hh:mm";
}

但是我的X轴仍然没有标签):

使用了以下代码:

修复我的问题。

var desc = values[0].Sensors.SensorType.Description;
var seriesName = String.Format("{0}'r'nSensorId: {1}", desc, sensorId);
curSeries = chartValues.Series.Add(seriesName);
curSeries.ChartType = SeriesChartType.Line;
curSeries.XValueType = ChartValueType.DateTime;
foreach (var item in values)
{
    curSeries.Points.AddXY(item.Timestamp, item.Value);
}

不显示MSChart x轴标签

看起来您是将每个点作为自己的系列添加。通常,你会在一个系列中添加许多点。

您打算在使用seriesId之前更改它吗?

   int seriesId = chartValues.Series.Count - 1;

这发生在你调用AddPointToChart()之前所以它得到的值与你在系列

中使用的值不同
chartValues.Series.Add(GenerateDefaultSeries(String.Format("{0}'r'nSensorId: {1}", desc, sensorId)));

编辑

行chartValues.Series.Add(…)将返回一个Series类型的变量,您可以直接添加它。见http://www.dotnetperls.com/chart

SensorID是你的号码,我认为没有保证系统会选择以任何特定的顺序填充它的系列集合。


编辑

看起来你在完成设置之前就将你的分数添加到系列中。相反,将它们全部设置好,然后添加它们:

private void AddPointToChart(DateTime timestamp, double value, int series)
{
    DataPoint dataPoint = new DataPoint(timestamp.ToOADate(), value);
    dataPoint.AxisLabel = "dd.MM.yy hh:mm";  // not sure how this will work, but try it.
    chart1.Series[series].Points.Add(dataPoint );
}