不显示MSChart X轴标签

时间:2011-12-19 17:23:31

标签: c# mschart

我目前正在构建一个显示折线图并使用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-axies标签是正确的 Click for pic
但是,如果我启动我的应用程序并以编程方式添加一个系列,它就像this

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

编辑: 根据汤姆的帮助,我改变了一些事情:

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轴stil没有标签):

使用以下代码:

解决了我的问题。

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);
}

1 个答案:

答案 0 :(得分:2)

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

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

   int seriesId = chartValues.Series.Count - 1;

在调用AddPointToChart()之前会发生这种情况,因此获得的值与系列中使用的值不同

chartValues.Series.Add(GenerateDefaultSeries(String.Format("{0}\r\nSensorId: {1}", desc, sensorId)));

修改

line 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 );
}