【问题标题】:MS Chart - How to put the X label on the left for a column seriesMS Chart - 如何将 X 标签放在列系列的左侧
【发布时间】:2013-09-17 23:54:04
【问题描述】:

我目前正在处理这张图(抱歉,这是我的第一篇文章,所以我不能直接发布图片):

Graph I am working on

对于一个给定的小时,例如 10 小时,我的 Y 值表示 10 小时到 11 小时之间的请求数。

正如您在我的图表中看到的,这些列以它们的 X 标签为中心。我的问题很简单,我怎么能把标签放在列的左边,所以在我的图表上,所有的列都在两个标签之间。

在简历中,我正在寻找一种在每一列上执行此操作的方法:

What I am looking for

如果你需要这个,这里是我的代码中关于这个图表的相关行

    myAdapter.Fill(DailyData);

    // Add points to the series
    for (int i = 0; i < DailyData.Rows.Count; i++)
    {
        DataRow row = DailyData.Rows[i];
        if (int.Parse(row["Hours"].ToString()) < 10)
        {
            DailyChart.Series["Series1"].Points.AddXY("0" + row["Hours"].ToString() + "h", row["RequestsNumber"].ToString());
        }
        else
        {
            DailyChart.Series["Series1"].Points.AddXY(row["Hours"].ToString() + "h", row["RequestsNumber"].ToString());
        }
    }

    // Set series chart type
    DailyChart.Series["Series1"].ChartType = SeriesChartType.Column;
    DailyChart.Series["Series1"]["PointWidth"] = "1";

    // Set X axis labels format
    DailyChart.ChartAreas["ChartArea1"].AxisX.Interval = 1;

祝你有美好的一天!

【问题讨论】:

    标签: c# visual-studio-2008 mschart


    【解决方案1】:

    我知道 Microsoft for MSChart 的 WinFormsChartSample 有一个名为 HistogramHelper.vb 的文件,如果我没记错的话,它可以解决您的问题。

    我个人使用了这段代码的稍微修改过的版本,我会放在这里:

    HistogramHelper.vb

    Imports System
    Imports System.Drawing
    Imports System.Drawing.Drawing2D
    Imports System.Windows.Forms.DataVisualization.Charting
    Imports System.Collections
    
    '--------------------------------------------------------------------------------------
    'Code extracted from the WinFormsChartSample from Microsoft
    'Modified to only use a single Series obj, instead of two with one of them hidden
    'Also removed out the Auto Labels and % Axis
    '--------------------------------------------------------------------------------------
    
    
    ''' <summary>
    ''' Helper class that creates a histogram chart. Histogram is a data
    ''' distribution chart which shows how many values, from the data series,
    ''' are inside each segment interval.  
    ''' 
    ''' You can define how many intervals you want to have using the SegmentIntervalNumber
    ''' field or the exact length of the interval using the SegmentIntervalWidth
    ''' field. Actual segment interval number can be slightly different due
    ''' to the automatic interval rounding.
    ''' </summary>
    Public Class HistogramChartHelper
    #Region "Fields"
    
        ''' <summary>
        ''' Number of class intervals the data range is devided in.
        ''' This property only has affect when "SegmentIntervalWidth" is 
        ''' set to double.NaN.
        ''' </summary>
        Public SegmentIntervalNumber As Integer = 20
    
        ''' <summary>
        ''' Histogram class interval width. Setting this value to "double.NaN"
        ''' will result in automatic width calculation based on the data range
        ''' and number of required interval specified in "SegmentIntervalNumber".
        ''' </summary>
        Public SegmentIntervalWidth As Double = Double.NaN
    
        ''' <summary>
        ''' Indicates that percent frequency should be shown on the right axis
        ''' </summary>
        Public ShowPercentOnSecondaryYAxis As Boolean = True
    
    #End Region   ' Fields
    
    #Region "Methods"
    
        ''' <summary>
        ''' Creates a histogram chart.
        ''' </summary>
        ''' <param name="chartControl">Chart control reference.</param>
        ''' <param name="dataPoints">Original Data Series</param>
        ''' <param name="histogramSeriesName">Name of the histogram series.</param>
        Public Function CreateHistogram(ByVal chartControl As Chart, ByVal dataPoints() As Double, ByVal histogramSeriesName As String) As Series
            ' Validate input
            If chartControl Is Nothing Then
                Debug.Print("Invalid chart control passed")
                Return Nothing
            End If
    
    
            Dim aSeries As New Series
    
            ' Set new series chart type and other attributes
            aSeries.ChartType = SeriesChartType.Column
            aSeries.BorderColor = Color.Black
            aSeries.BorderWidth = 1
            aSeries.BorderDashStyle = ChartDashStyle.Solid
    
    
            ' Get data series minimum and maximum values
            Dim minValue As Double = Double.MaxValue
            Dim maxValue As Double = Double.MinValue
            Dim pointCount As Integer = 0
    
            For i As Integer = 0 To dataPoints.Length - 1
    
                ' Process only non-empty data points
    
                If dataPoints(i) > maxValue Then
                    maxValue = dataPoints(i)
                End If
                If dataPoints(i) < minValue Then
                    minValue = dataPoints(i)
                End If
                pointCount += 1
    
            Next
    
            ' Calculate interval width if it's not set
            If Double.IsNaN(Me.SegmentIntervalWidth) Then
                Me.SegmentIntervalWidth = (maxValue - minValue) / SegmentIntervalNumber
                Me.SegmentIntervalWidth = RoundInterval(Me.SegmentIntervalWidth)
            End If
    
            ' Round minimum and maximum values
            minValue = Math.Floor(minValue / Me.SegmentIntervalWidth) * Me.SegmentIntervalWidth
            maxValue = Math.Ceiling(maxValue / Me.SegmentIntervalWidth) * Me.SegmentIntervalWidth
    
            ' Create histogram series points
            Dim currentPosition As Double = minValue
            currentPosition = minValue
            Do While currentPosition <= maxValue
                ' Count all points from data series that are in current interval
                Dim count As Integer = 0
    
                For i As Integer = 0 To dataPoints.Length - 1
    
                    Dim endPosition As Double = currentPosition + Me.SegmentIntervalWidth
                    If dataPoints(i) >= currentPosition AndAlso dataPoints(i) < endPosition Then
                        count += 1
    
                        ' Last segment includes point values on both segment boundaries
                    ElseIf endPosition > maxValue Then
                        If dataPoints(i) >= currentPosition AndAlso dataPoints(i) <= endPosition Then
                            count += 1
                        End If
                    End If
    
                Next
    
    
                ' Add data point into the histogram series
                aSeries.Points.AddXY(currentPosition + Me.SegmentIntervalWidth / 2.0, count)
                currentPosition += Me.SegmentIntervalWidth
            Loop
    
            ' Adjust series attributes
            'This is a "Custom Property" http://msdn.microsoft.com/en-us/library/dd456700.aspx
            aSeries("PointWidth") = "1"
    
            ' Adjust chart area
            Dim chartArea As ChartArea = chartControl.ChartAreas(0)
            'chartArea.AxisY.Title = "Frequency"
            chartArea.AxisX.Minimum = minValue
            chartArea.AxisX.Maximum = maxValue
    
            ' Set axis interval based on the histogram class interval
            ' and do not allow more than 10 labels on the axis.
            Dim axisInterval As Double = Me.SegmentIntervalWidth
            Do While (maxValue - minValue) / axisInterval > 10.0
                axisInterval *= 2.0
            Loop
            chartArea.AxisX.Interval = axisInterval
    
            Return aSeries
        End Function
    
        ''' <summary>
        ''' Helper method which rounds specified axsi interval.
        ''' </summary>
        ''' <param name="interval">Calculated axis interval.</param>
        ''' <returns>Rounded axis interval.</returns>
        Friend Function RoundInterval(ByVal interval As Double) As Double
            ' If the interval is zero return error
            If interval = 0.0 Then
                Throw (New ArgumentOutOfRangeException("interval", "Interval can not be zero."))
            End If
    
            ' If the real interval is > 1.0
            Dim step_Renamed As Double = -1
            Dim tempValue As Double = interval
            Do While tempValue > 1.0
                step_Renamed += 1
                tempValue = tempValue / 10.0
                If step_Renamed > 1000 Then
                    Throw (New InvalidOperationException("Auto interval error due to invalid point values or axis minimum/maximum."))
                End If
            Loop
    
            ' If the real interval is < 1.0
            tempValue = interval
            If tempValue < 1.0 Then
                step_Renamed = 0
            End If
    
            Do While tempValue < 1.0
                step_Renamed -= 1
                tempValue = tempValue * 10.0
                If step_Renamed < -1000 Then
                    Throw (New InvalidOperationException("Auto interval error due to invalid point values or axis minimum/maximum."))
                End If
            Loop
    
            Dim tempDiff As Double = interval / Math.Pow(10.0, step_Renamed)
            If tempDiff < 3.0 Then
                tempDiff = 2.0
            ElseIf tempDiff < 7.0 Then
                tempDiff = 5.0
            Else
                tempDiff = 10.0
            End If
    
            ' Make a correction of the real interval
            Return tempDiff * Math.Pow(10.0, step_Renamed)
        End Function
    
    #End Region   ' Methods
    End Class
    

    然后我有一个帮助函数来生成直方图:

     Public Function hist(ByVal x As Double(), Optional ByVal bins As Integer = 10) As System.Windows.Forms.DataVisualization.Charting.Series
    
            ' HistogramChartHelper is a helper class found in the samples Utilities folder. 
            Dim histogramHelper As New HistogramChartHelper()
    
            ' Specify number of segment intervals
            histogramHelper.SegmentIntervalNumber = bins
    
            ' Create histogram series    
            Dim newSeries As Series = histogramHelper.CreateHistogram(gca(), x, "Histogram")
            gca().ChartAreas(0).AxisX.IsLogarithmic = False
            gca().ChartAreas(0).AxisY.IsLogarithmic = False
            gca().Series.Add(newSeries)
            gcf().DoRefresh()
    
            Return newSeries
        End Function
    

    并尝试生成类似于您的直方图: hist({8,8,8,8,8,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,11,11,11,12,12,12,12,12,13,13,14,14,14,14,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15,15,16,16,16,16,16,16,16,16,17,17,17,17,17,17,17,17,18,18})

    我得到:

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多