【问题标题】:Set Chart Width to 70%将图表宽度设置为 70%
【发布时间】:2016-01-05 10:14:43
【问题描述】:

有谁知道如何将图表宽度设置为容器的70%?我已经将容器宽度设置为 100%,如下所示:container.Width = Unit.Percentage(100);

这是我的代码:

                    Chart Chart1= new Chart();
                    Chart1.DataSource = dt;
                    Chart1.Width = 800;
                    Chart1.Height = 490;                  

                    Chart1.Series.Add(new Series());
                    Chart1.Series[0].ChartType = SeriesChartType.BoxPlot;
                    List<object> List1 = dt.AsEnumerable().ToList<object>();

                    foreach (DataRow row in dt.Rows)
                        Chart1.Series[0].Points.AddXY(row["STATUS"], new object[] { row["MIN"], row["MAX"], row["25THPERCENTILE"], row["75THPERCENTILE"], row["AVG"], row["50THPRECENTILE"] });

                    //create chartareas
                    ChartArea ca = new ChartArea();
                    ca.AxisX = new Axis();
                    ca.AxisX.MajorGrid.Enabled = false;
                    ca.AxisY = new Axis();
                    ca.AxisY.MajorGrid.Enabled = false;
                    Chart1.ChartAreas.Add(ca);

                    //databind
                    Chart1.DataBind();
                    Chart1.Visible = true;

                    panel.Controls.Add(Chart1);

现在图表宽度设置为像素。我想将图表宽度设置为 percentage

问题:如何将图表宽度设置为70%

【问题讨论】:

  • 什么的 70%,是800 的 70% 还是页面、容器等的 70%?
  • 嗨@Spencer Wieczorek,是容器的70%。我已经将容器宽度设置为 100%。像这样:container.Width = Unit.Percentage(100);,已经编辑了我的问题,谢谢!
  • 如果能得到容器的大小,来算一算:Chart1.Width = (0.70 * container.Width)
  • 嗨@Dyrandz Famador,已尝试使用此方法,但出现以下错误:运算符'*'不能应用于'double'和'System.Web.UI.WebControls.Unit类型的操作数'
  • 0.70..中添加后缀m 像这样:Chart1.Width = (0.70m * container.Width) - 没有后缀m,数字被视为双精度,从而产生编译错误 i>.

标签: javascript c# css asp.net charts


【解决方案1】:

显然Chart 控件只接受UnitType.Pixel

这很好用:

 Unit unit1 = new Unit(70, UnitType.Pixel);
 Chart1.Width = unit1;

但是这个,

 Unit unit2 = new Unit(70, UnitType.Percentage);
 Chart1.Width = unit2;

抛出异常:

System.ArgumentException 未被用户代码处理 H结果=-2147024809 Message=图表宽度必须以像素为单位。 源=System.Web.DataVisualization 堆栈跟踪: 在 System.Web.UI.DataVisualization.Charting.Chart.set_Width(单位值) 在 C:\Users\WebApplication63\WebApplication63\WebForm1.aspx.cs:line 16 中的 WebApplication63.WebForm1.Page_Load(Object sender, EventArgs e) 在 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(对象发送者,EventArgs e) 在 System.Web.UI.Control.OnLoad(EventArgs e) 在 System.Web.UI.Control.LoadRecursive() 在 System.Web.UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,布尔 includeStagesAfterAsyncPoint) 内部异常:

【讨论】:

  • 嗨 jstreet,这是否意味着我只能以像素为单位设置图表宽度?有没有办法将图表宽度设置为百分比?感谢您的回复和帮助!
  • @FeliciaSoh,嗨,不幸的是,看起来就是这样......异常消息再清楚不过了。
  • 嗨@jstreet,好的明白了,谢谢你的解释!
  • 您可以自己尝试并验证它......或者其他人可能有其他想法,但我对此表示怀疑......代码非常简单,例外也是如此。
【解决方案2】:

你不能为图表指定一个客户端Id并用css指定宽度吗?

<asp:Chart ID="myChart" runat="server" ClientIDMode="Static">

然后在你的 css 中:

#myChart{
    width:70%;
}  

【讨论】:

    猜你喜欢
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 2012-09-30
    相关资源
    最近更新 更多