简体   繁体   English

MS图表-如何在列系列的左侧放置X标签

[英]MS Chart - How to put the X label on the left for a column series

I am currently working on this graph (sorry it's my first post, so with my reputation I can't post image directly) : 我目前正在处理这张图(很抱歉,这是我的第一篇文章,因此,以我的声誉,我无法直接发布图片):

Graph I am working on 我正在处理的图

For a giving hour, for example 10h, my Y value represents a number of requests between 10h and 11h. 对于一个给定的小时,例如10h,我的Y值表示10h和11h之间的请求数。

As you can see on my graph, the columns are centered on their X label. 正如您在我的图表上看到的那样,列位于其X标签的中心。 My question is quite simple, how could I put the label on the left of the column, so on my graph all the columns will be between two labels. 我的问题很简单,如何将标签放在列的左侧,因此在我的图形上所有列都将位于两个标签之间。

In resume, I am looking for a way to do this on every columns : 在简历中,我正在寻找一种在每一列上执行此操作的方法:

What I am looking for 我在寻找什么

In case you need this, here are the pertinent lines of my code about this graph 如果您需要此,这是我的关于此图的代码的相关行

    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;

Have a good day! 祝你有美好的一天!

I know the WinFormsChartSample from Microsoft for MSChart has a file called HistogramHelper.vb which if I recall correctly solves your problem. 我知道Microsoft为MSChart提供的WinFormsChartSample有一个名为HistogramHelper.vb的文件,如果我记得正确的话,可以解决您的问题。

I personally use a slightly modified version of this code, which I'll put here: 我个人使用了此代码的稍微修改后的版本,将在这里输入:

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

I then have a Helper function to generate histograms: 然后,我有了一个Helper函数来生成直方图:

 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

And trying to generate a Histogram similar to yours: 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}) 并尝试生成类似于您的直方图: 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})

I get: 我得到: 生成的直方图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM