wpf - 绘制柱形图,柱与柱之间没有空间

标签 wpf graph wpftoolkit charts silverlight-toolkit

我正在使用 WPF 工具包,并尝试渲染一个看起来像直方图的图形。特别是,我希望每一列都与其他列相对应。列之间不应有间隙。

创建柱形图时需要应用许多组件。 (请参阅下面的示例 XAML)。有谁知道是否可以在其中一个元素上设置一个属性,该属性指的是列之间的空白宽度?

                <charting:Chart Height="600" Width="Auto" HorizontalAlignment="Stretch" Name="MyChart"
                    Title="Column Graph" LegendTitle="Legend">

                    <charting:ColumnSeries 
                        Name="theColumnSeries"
                        Title="Series A"
                        IndependentValueBinding="{Binding Path=Name}"                
                        DependentValueBinding="{Binding Path=Population}"
                        Margin="0"
                        >
                    </charting:ColumnSeries>

                    <charting:Chart.Axes>
                        <charting:LinearAxis 
                            Orientation="Y" 
                            Minimum="200000" 
                            Maximum="2500000" 
                            ShowGridLines="True" />
                        <charting:CategoryAxis
                            Name="chartCategoryAxis"
                            />
                    </charting:Chart.Axes>
                </charting:Chart>

最佳答案

由于没有神奇的答案,我从 codeplex 下载了 wpftoolkit 代码。

通过阅读代码,我可以看到在ColumnSeries.UpdateDataPoint方法中,有这样一行代码:

    double segmentWidth = coordinateRangeWidth * 0.8;

所以这是一个非常明确的“不”,您不能通过设置公共(public)属性来更改列之间的间隙。

我要尝试的解决方案是编写一个继承自ColumnSeries的新类,并覆盖UpdateDataPoint

<小时/>

稍后编辑

好的,我已经开始工作了。如果有人感兴趣,我附上了 HistogramSeries 类的完整代码。

public class HistogramSeries : ColumnSeries, ISeries
{
    protected override void UpdateDataPoint(DataPoint dataPoint)
    {
        // That set the height and width.
        base.UpdateDataPoint(dataPoint);
        // Now we override the part about setting the width
        object category = dataPoint.ActualIndependentValue;
        var coordinateRange = GetCategoryRange(category);
        double minimum = (double)coordinateRange.Minimum.Value;
        double maximum = (double)coordinateRange.Maximum.Value;
        double coordinateRangeWidth = (maximum - minimum);
        const int WIDTH_MULTIPLIER = 1; // Harcoded to 0.8 in the parent. Could make this a dependency property
        double segmentWidth = coordinateRangeWidth * WIDTH_MULTIPLIER;
        var columnSeries = SeriesHost.Series.OfType<ColumnSeries>().Where(series => series.ActualIndependentAxis == ActualIndependentAxis);
        int numberOfSeries = columnSeries.Count();
        double columnWidth = segmentWidth / numberOfSeries;
        int seriesIndex = columnSeries.IndexOf(this);
        double offset = seriesIndex * Math.Round(columnWidth) + coordinateRangeWidth * 0.1;
        double dataPointX = minimum + offset;
        double left = Math.Round(dataPointX);
        double width = Math.Round(columnWidth);
        Canvas.SetLeft(dataPoint, left);
        dataPoint.Width = width;
    }
    #region ISeries Members
    System.Collections.ObjectModel.ObservableCollection<object> ISeries.LegendItems
    {
        get { return base.LegendItems; }
    }
    #endregion
    #region IRequireSeriesHost Members
    ISeriesHost IRequireSeriesHost.SeriesHost
    {
        get { return base.SeriesHost;}
        set { base.SeriesHost = value; }
    }
    #endregion
}
// Copied from the DataVisualization library
// (It was an internal class)
static class MyEnumerableFunctions
{
    public static int IndexOf(this IEnumerable that, object value)
    {
        int index = 0;
        foreach (object item in that)
        {
            if (object.ReferenceEquals(value, item) || value.Equals(item))
            {
                return index;
            }
            index++;
        }
        return -1;
    }
}

关于wpf - 绘制柱形图,柱与柱之间没有空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2755960/

相关文章:

.net - 使用哪个调度程序?窗口还是控件?

c# - Xaml:MouseLeftButtonUp 事件不起作用

algorithm - 什么样的图来表示这个业务逻辑决策树?

graph - 从 Clojure 中表示为向量的树中获取边

WPF 工具包图表 - 折叠图表点

c# - Caliburn.Micro WindowManager找不到 View

wpf - 使用动画制作 WPF 标签(或其他元素)闪光

c# - 在加权图 C# 实现中查找最大权重团

c# - WPF 工具包 DataGrid 列调整大小事件

wpf - 更改绑定(bind)属性的值时如何为文本 block 的背景设置动画?