力点之间的间隙在x轴上(MS的.Net图表控件,柱形图) [英] Force a gap between points on the x axis (MS .Net Chart Controls, Column Chart)

查看:113
本文介绍了力点之间的间隙在x轴上(MS的.Net图表控件,柱形图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个柱形图具有多个系列的每款包含多个点。目前,列所有相互接触。我想强制每列之间的间隙。我怎样才能做到这一点?

我发现,应用PointWidth( Chart1.Series [seriesName] [PointWidth] =(0.6)的ToString(); )给我的一个分离x值组,但不是在一个单独的组中的每个系列点(我需要)之间。使用空隔系列作为<一个href="http://social.msdn.microsoft.com/Forums/fi-FI/MSWinWebChart/thread/38640bc4-0ea2-427a-b758-eaad8e44e321"相对=nofollow>别处建议的不解决问题。

我使用.NET 4.0,VS 2010,Web应用程序。我的表code如下:

 使用系统;
使用System.Collections.Generic;
使用System.Drawing中;
使用System.Linq的;
使用System.Web.UI程序;
使用System.Web.UI.DataVisualization.Charting;

命名空间WebApplication1
{
    公共部分类_Default:页
    {
        保护无效的Page_Load(对象发件人,EventArgs的)
        {
            Chart1.ChartAreas.Add(默认);
            Chart1.ChartAreas [默认] =背景色Color.White。
            Chart1.ChartAreas [默认] BackSecondaryColor = Color.AliceBlue。
            Chart1.ChartAreas [默认] BackGradientStyle = GradientStyle.TopBottom。

            Chart1.BackColor = Color.AliceBlue;
            Chart1.BackSecondaryColor = Color.White;
            Chart1.BackGradientStyle = GradientStyle.TopBottom;
            Chart1.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
            VAR颜色=新的名单,其中,彩色&GT;(GetSystemColors()式(C =&GT; c.Name.StartsWith(黑暗)));
            VAR RNG =新的随机();
            VAR开始= rng.Next(0,colors.Count  -  1);
            对于(VAR C =启动;℃下开始+ 6; C ++)
            {
                VAR颜色=颜色[C%colors.Count]。
                Chart1.Series.Add(color.Name);
                Chart1.Series [color.Name] .BorderColor =颜色;
                Chart1.Series [color.Name] .BorderWidth = 1;
                Chart1.Series [color.Name]。颜色= Color.FromArgb((INT)(255 * 0.7),颜色);
                Chart1.Series [color.Name] .BackSecondaryColor = Color.White;
                Chart1.Series [color.Name] .BackGradientStyle = GradientStyle.TopBottom;
                对于(VAR年= DateTime.Now.AddYears(-5).Year;一年&LT; D​​ateTime.Now.Year;同期++)
                    Chart1.Series [color.Name] .Points.Add(新数据点(年,rng.Next(0,20)));

                Chart1.Series [color.Name] [PointWidth] =(0.6)的ToString();
                //Chart1.Series.Add("Spacer:+ color.Name);
                //Chart1.Series["Spacer:+ color.Name] [PointWidth] =(0.6)的ToString();
            }
            Chart1.Legends.Add(默认);
        }

        静态的IEnumerable&LT;颜色&GT; GetSystemColors()
        {
            类型类型= ty​​peof运算(彩色);
            返回type.GetProperties()式。(信息=&GT; info.PropertyType ==型)。选择(资讯=&GT;(彩色)info.GetValue(NULL,NULL));
        }
    }
}
 

解决方案

我有鬼子自己的工作再现您的具体情况。我想帮助,因为我想我可以学到一些东西通过这样做,但我需要你的标记,或者更好的,整体解决方案!我通过当我试图一个简单的页面与图表了ChartImg.axd错误执行子请求挣扎。我发现我需要添加一个处理程序的配置。我再通过未能加载程序集System.Web.DataVisualization战斗,因为我复制的处理元素所引用的3.5 DataVisualization总成,使我改变了,要4.0终于看到了一个曲线图。那是!有什么工作

您垫片系列不产生间隙的原因是因为在该系列没有值。通知的最后两行的下面code,即加零值到垫片系列。这将产生等系列之间所需的间隔,而且你还会发现在图例中,如果你有一个垫片系列,这是丑陋的,至少可以说。

 为(变种C =启动;℃下开始+ 6; C ++)
  {
   VAR颜色=颜色[C%colors.Count]。
   VAR seriesName =系列+ C; // color.Name);
   Chart1.Series.Add(seriesName);
   Chart1.Series [seriesName] .BorderColor =颜色;
   Chart1.Series [seriesName] .BorderWidth = 1;
   Chart1.Series [seriesName]。颜色= Color.FromArgb((INT)(255 * 0.7),颜色);
   Chart1.Series [seriesName] .BackSecondaryColor = Color.FromArgb((INT)(255 * 0.2),颜色);
   Chart1.Series [seriesName] .BackGradientStyle = GradientStyle.TopBottom;
   对于(VAR年= DateTime.Now.AddYears(-5).Year;一年&LT; D​​ateTime.Now.Year;同期++)
    Chart1.Series [seriesName] .Points.Add(新数据点(年,rng.Next(0,20)));

   Chart1.Series [seriesName] [PointWidth] =(0.6)的ToString();

   seriesName =间隔:+ seriesName;
   Chart1.Series.Add(seriesName);
   Chart1.Series [seriesName] [PointWidth] =(0.6)的ToString();
   对于(VAR年= DateTime.Now.AddYears(-5).Year;一年&LT; D​​ateTime.Now.Year;同期++)
    Chart1.Series [seriesName] .Points.Add(新的数据点(年,0));
  }
 

您可以设置图例文本的空间(特别注意:空字符串被忽略,图例文本没有被设置),如下所示,但联想仍然会显示这些垫片系列。

  Chart1.Series [seriesName] .LegendText =;
 

如果你幸运的话,你会不会需要显示的传说,也可以设置间隔系列的颜色相同的颜色作为背景的​​传奇和传说的文本空间。这导致在双间距外表,其中很可能是可接受的图例

I have a column chart with multiple series each containing multiple points. Currently the columns all touch each other. I want to force a gap between each column. How can I achieve this?

I found that applying a PointWidth (Chart1.Series[seriesName]["PointWidth"] = (0.6).ToString();) gives me a separation between the x value groups but not between each series point in an individual group (which I need). Using an empty spacer series as suggested elsewhere does not solve the problem.

I am using .Net 4, VS 2010, Web Application. My chart code follows:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web.UI;
using System.Web.UI.DataVisualization.Charting;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Chart1.ChartAreas.Add("Default");
            Chart1.ChartAreas["Default"].BackColor = Color.White;
            Chart1.ChartAreas["Default"].BackSecondaryColor = Color.AliceBlue;
            Chart1.ChartAreas["Default"].BackGradientStyle = GradientStyle.TopBottom;

            Chart1.BackColor = Color.AliceBlue;
            Chart1.BackSecondaryColor = Color.White;
            Chart1.BackGradientStyle = GradientStyle.TopBottom;
            Chart1.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
            var colors = new List<Color>(GetSystemColors().Where(c=>c.Name.StartsWith("Dark")));
            var rng = new Random();
            var start = rng.Next(0, colors.Count - 1);
            for (var c = start; c < start + 6; c++)
            {
                var color = colors[c % colors.Count];
                Chart1.Series.Add(color.Name);
                Chart1.Series[color.Name].BorderColor = color;
                Chart1.Series[color.Name].BorderWidth = 1;
                Chart1.Series[color.Name].Color = Color.FromArgb((int)(255 * .7), color);
                Chart1.Series[color.Name].BackSecondaryColor = Color.White;
                Chart1.Series[color.Name].BackGradientStyle = GradientStyle.TopBottom;
                for (var year = DateTime.Now.AddYears(-5).Year; year < DateTime.Now.Year; year++)
                    Chart1.Series[color.Name].Points.Add(new DataPoint(year, rng.Next(0, 20)));

                Chart1.Series[color.Name]["PointWidth"] = (0.6).ToString();
                //Chart1.Series.Add("Spacer:" + color.Name);
                //Chart1.Series["Spacer:" + color.Name]["PointWidth"] = (0.6).ToString();
            }
            Chart1.Legends.Add("Default");
        }

        static IEnumerable<Color> GetSystemColors()
        {
            Type type = typeof(Color);
            return type.GetProperties().Where(info => info.PropertyType == type).Select(info => (Color)info.GetValue(null, null));
        }
    }
}

解决方案

I had the devils own job reproducing your situation. I wanted to help because I thought I could learn something by doing so, but I needed your markup, or better yet, the whole solution! I struggled through the "Error executing child request for ChartImg.axd" when I tried a simple page with a chart. I discovered I needed to add a handler in the config. I then fought through the failure to load assembly System.Web.DataVisualization because the handler element I copied referenced the 3.5 DataVisualization assembly so I changed that to 4.0 and finally saw a graph. What a job THAT was!

The reason your spacer series is not creating a gap is because there are no values in that series. Notice the last two lines of code below, that add zero values to the spacer series. This produces the desired gap between the other series, but you will also find the spacer series listed in your legend if you have one, which is ugly to say the least.

  for (var c = start; c < start + 6; c++)
  {
   var color = colors[c % colors.Count];
   var seriesName = "Series "+ c;//color.Name);
   Chart1.Series.Add(seriesName);
   Chart1.Series[seriesName].BorderColor = color;
   Chart1.Series[seriesName].BorderWidth = 1;
   Chart1.Series[seriesName].Color = Color.FromArgb((int)(255 * .7), color);
   Chart1.Series[seriesName].BackSecondaryColor = Color.FromArgb((int)(255 * .2), color);
   Chart1.Series[seriesName].BackGradientStyle = GradientStyle.TopBottom;
   for (var year = DateTime.Now.AddYears(-5).Year; year < DateTime.Now.Year; year++)
    Chart1.Series[seriesName].Points.Add(new DataPoint(year, rng.Next(0, 20)));

   Chart1.Series[seriesName]["PointWidth"] = (0.6).ToString();

   seriesName = "Spacer:" + seriesName;
   Chart1.Series.Add(seriesName);
   Chart1.Series[seriesName]["PointWidth"] = (0.6).ToString();
   for (var year = DateTime.Now.AddYears(-5).Year; year < DateTime.Now.Year; year++)
    Chart1.Series[seriesName].Points.Add(new DataPoint(year, 0));
  }

You can set the legend text to a space (NB. empty string is ignored and legend text is not set) as follows, but the Legend will still show these spacer series.

    Chart1.Series[seriesName].LegendText = " ";

If you're lucky, you won't need to show the legend, or you can set the spacer series colours to the same colour as the legend background and the legend text to spaces. This results in a double-spacing look in the Legend which is likely to be acceptable.

这篇关于力点之间的间隙在x轴上(MS的.Net图表控件,柱形图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆