I want to add a spline graphic and a points graphic on the same asp.net chart. Can somebody show an explicite example of how you do that?
Thank you.
Would be nice to know if you want a C# or a Visual Basic example. I've got a Visual Basic one for you. (You could convert it to C# with an online converter). I hope this helps you.
.aspx file:
<form id="form1" runat="server">
<div>
<asp:Chart ID="Chart1" runat="server">
<ChartAreas>
<asp:ChartArea Name="ChartArea1"></asp:ChartArea>
</ChartAreas>
</asp:Chart>
</div>
</form>
.vb file:
Imports System.Web.UI.DataVisualization.Charting
Imports System.Drawing
Public Class ChartExample
Inherits System.Web.UI.Page
Private Sub Page_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Add the title
Chart1.Titles.Add("Title1")
' Set the text of the title
Chart1.Titles("Title1").Text = "Chart Area 1 Title"
' Dock the title to a ChartArea
Chart1.Titles("Title1").DockedToChartArea = "ChartArea1"
' Creating the series
Dim series1 As New Series("Series1")
Dim series2 As New Series("Series2")
' Setting the Chart Type
series1.ChartType = SeriesChartType.Spline
series2.ChartType = SeriesChartType.Point
' Adding some points
series1.Points.AddXY(0, 10)
series1.Points.AddXY(5, 16)
series1.Points.AddXY(10, 9)
series1.Points.AddXY(15, 32)
series1.Points.AddXY(20, 21)
series2.Points.AddXY(0, 5)
series2.Points.AddXY(5, 12)
series2.Points.AddXY(10, 18)
series2.Points.AddXY(15, 11)
series2.Points.AddXY(20, 25)
' Add the series to the chart
Chart1.Series.Add(series1)
Chart1.Series.Add(series2)
' Set the chart's height and width
Chart1.Width = 600
Chart1.Height = 600
' Setting the X Axis
Chart1.ChartAreas("ChartArea1").AxisX.IsMarginVisible = True
Chart1.ChartAreas("ChartArea1").AxisX.Interval = 1
Chart1.ChartAreas("ChartArea1").AxisX.Maximum = [Double].NaN
Chart1.ChartAreas("ChartArea1").AxisX.Title = "x"
Chart1.ChartAreas("ChartArea1").AxisX.TitleFont = New Font("Sans Serif", 10, FontStyle.Bold)
' Setting the Y Axis
Chart1.ChartAreas("ChartArea1").AxisY.Interval = 2
Chart1.ChartAreas("ChartArea1").AxisY.Maximum = [Double].NaN
Chart1.ChartAreas("ChartArea1").AxisY.Title = "y"
Chart1.ChartAreas("ChartArea1").AxisY.TitleFont = New Font("Sans Serif", 10, FontStyle.Bold)
End Sub
End Class
Related
I have been trying to solve this issue from some time now. The symptom is with the X axis I see that the value get overrided to a lower value from what I have set it at in Chart1.ChartAreas(0).AxisX.Minimum = Xmin (8.5 in this case) . However when I times the values by 100 the curve seems to center to a min and max. I am also not experiencing this with the Y axis at all. In the picture attached I am setting the min to 8.5 ( as seen where the curve starts in the picture. I also notice if I set the min below 8 it will let me set it to that. So this leads me to believe there is some sort of value that its overriding ratio to make the minimum inside of MSchart.
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.IO
Imports System.Drawing
Public Class Test
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim value As Double
Dim xxmax As Double
Dim yymax As Double
Dim xxmin As Double
Dim yymin As Double
Dim XPlot As New ArrayList
Dim YPlot As New ArrayList
XPlot.Add(10.1)
XPlot.Add(10.15)
XPlot.Add(10.2)
XPlot.Add(10.25)
XPlot.Add(10.3)
YPlot.Add(1)
YPlot.Add(2)
YPlot.Add(3)
YPlot.Add(4)
YPlot.Add(5)
xxmin = 10.1
xxmax = 10.3
yymin = 1
yymax = 5
Chart1.Series.Clear()
Chart1.Series.Add("Polfit Fit")
Chart1.Series("Polfit Fit").XValueType = System.Web.UI.DataVisualization.Charting.ChartValueType.Int32
Chart1.Series("Polfit Fit").ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Line
Chart1.Series.Add("Plotted Data")
Chart1.Series("Plotted Data").XValueType = System.Web.UI.DataVisualization.Charting.ChartValueType.Int32
Chart1.Series("Plotted Data").ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Point
Chart1.Series("Plotted Data").MarkerStyle = System.Web.UI.DataVisualization.Charting.MarkerStyle.Circle
Chart1.Series("Plotted Data").MarkerSize = 10
Chart1.Series("Plotted Data").MarkerColor = Color.Green
Chart1.Series("Polfit Fit").Color = Color.Blue
Dim mylegend As New System.Web.UI.DataVisualization.Charting.Legend
mylegend.Font = New System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Bold)
mylegend.Name = "Legend"
mylegend.BorderWidth = 4
mylegend.BorderColor = Color.Black
mylegend.Docking = DataVisualization.Charting.Docking.Right
Chart1.Legends.Add(mylegend)
Chart1.ChartAreas(0).AxisX.Title = Value
Chart1.ChartAreas(0).AxisX.TitleFont = New System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Bold)
Chart1.ChartAreas(0).AxisY.TitleFont = New System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Bold)
Chart1.ChartAreas(0).AxisY.Title = "Margin Of Safety"
Chart1.ChartAreas(0).AxisX.IsMarginVisible = True
Chart1.ChartAreas(0).AxisY.IsMarginVisible = True
Chart1.ChartAreas(0).AxisY.IsStartedFromZero = False
Chart1.ChartAreas(0).AxisX.IsStartedFromZero = False
Chart1.ChartAreas(0).AxisX.Interval = 0.05
Chart1.ChartAreas(0).AxisY.Interval = 0.05
Chart1.ChartAreas(0).AxisX.Maximum = Math.Round((xxmax + Math.Abs(xxmax * 0.0)), 6)
Chart1.ChartAreas(0).AxisX.Minimum = Math.Round((xxmin - Math.Abs(xxmax * 0.0)), 6)
Chart1.ChartAreas(0).AxisY.Maximum = Math.Round((yymax + Math.Abs(yymax * 0.0)), 6)
Chart1.ChartAreas(0).AxisY.Minimum = Math.Round((yymin - Math.Abs(yymin * 0.0)), 6)
Chart1.Height = 600
Chart1.Width = 1500
Dim j
Dim b
While j < XPlot.Count
If Not YPlot(j) = "0" And Not (CDbl(XPlot(j)) / CDbl(YPlot(j)) = 1) Then
Dim point As New System.Web.UI.DataVisualization.Charting.DataPoint
point.SetValueXY((XPlot(j)), (YPlot(j)))
Chart1.Series("Plotted Data").Points.Add(point)
End If
j += 1
End While
b = 0
End Sub
End Class
<%# Page Language="VB" Inherits ="Test" CodeFile ="Test.vb" %>
<%# Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form runat="server" >
<asp:Chart ID="Chart1" runat="server">
<Series>
<asp:Series Name="Series1">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
</form>
</body>
</html>
Chart1.Series("Plotted Data").XValueType = System.Web.UI.DataVisualization.Charting.ChartValueType.Int32
should be
Chart1.Series("Plotted Data").XValueType = System.Web.UI.DataVisualization.Charting.ChartValueType.Double
That is what was causing the problem. Thank you Babback that solves the issue.
I am using MS Chart Control in ASP.Net using VB.Net. The chart type is Pie. I am facing a weird problem where the number of items displayed in the legend is square of the number of Series present. This means if I have 2 series added to the pie chart, 4 items are displayed in legend and if I have 7 series added to the pie chart, 49 items appear in the legend. The 1st set of 7 items displaying the correct data and the others just displaying 0.
Here is the markup of my chart control in ASPX -
<asp:Chart runat="server" ID="chartX" CssClass="chart" Width="420px" Height="500px" ImageType="Jpeg">
<Series></Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1"></asp:ChartArea>
</ChartAreas>
<Legends>
<asp:Legend Docking="Bottom" Alignment="Center" Font="Calibri"></asp:Legend>
</Legends>
</asp:Chart>
Here is the code to populate chart control in ASPX.vb -
Dim table As DataTable = PopulateData()
Dim dv As DataView = New DataView(table, "Count > 0", "", DataViewRowState.OriginalRows)
For Each r As DataRow In dv.ToTable().Rows
Dim series As String = r("Name").ToString()
chartX.Series.Add(series)
chartX.Series(series).ChartType = DataVisualization.Charting.SeriesChartType.Pie
chartX.Series(series).XValueMember = "Name"
chartX.Series(series).YValueMembers = "Count"
chartX.ChartAreas(0).Area3DStyle.Enable3D = True
chartX.Series(series).Label = "#VALX" & Environment.NewLine & "(#PERCENT)"
chartX.Series(series)("PieLabelStyle") = "Disabled"
Next
chartX.DataSource = dv
chartX.DataBind()
Hoping for any answers.
If I'm not mistaken, you are adding a new series for each row in you table. Instead you should be adding the series once for the table and then binding your data.
You do not need to iterate through all rows when setting up your graph. Do it only once binding to the table. I never touch VB, but it should looks something like this
Dim table As DataTable = PopulateData()
Dim dv As DataView = New DataView(table, "Count > 0", "", DataViewRowState.OriginalRows)
' setup just once
Dim series As String = "Series Name"
chartX.Series.Add(series)
chartX.Series(series).ChartType = DataVisualization.Charting.SeriesChartType.Pie
chartX.Series(series).XValueMember = "Name"
chartX.Series(series).YValueMembers = "Count"
chartX.ChartAreas(0).Area3DStyle.Enable3D = True
chartX.Series(series).Label = "#VALX" & Environment.NewLine & "(#PERCENT)"
chartX.Series(series)("PieLabelStyle") = "Disabled"
' bind to the entire set
chartX.DataSource = dv
chartX.DataBind()
Not sure why the labels are not continuously switching.
I break-pointed it and it shows the counter resets everytime and so it should just be
continuously swapping through the numbers but it doesn't seem to be working
Thanks!
Public Class WebForm2 Inherits System.Web.UI.Page
Dim d As Integer() = {0, 1, 2, 3, 4}
Dim counter As Integer
Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Label1.Text = d(0)
Label2.Text = d(1)
Label3.Text = d(2)
Label4.Text = d(3)
Label5.Text = d(4)
End If
End Sub
Public Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim hold As Integer
counter = 0
hold = d(0)
Do While counter < 4
d(counter) = d(counter + 1)
counter += 1
Loop
Label1.Text = d(0)
Label2.Text = d(1)
Label3.Text = d(2)
Label4.Text = d(3)
Label5.Text = hold
End Sub
Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Dim hold As Integer
counter = 0
hold = d(0)
Do While counter < 4
d(counter) = d(counter + 1)
counter += 1
Loop
Label1.Text = d(0)
Label2.Text = d(1)
Label3.Text = d(2)
Label4.Text = d(3)
Label5.Text = hold
End Sub
End Class
I am not familiar with vb.net but I've created asp.net pages before so I am taking an educated guess here^^
When clicking the button a postback happens and the whole page gets reloaded and the array d is declared again.
The values you store in your array are lost after the Post-Back. For keeping data you could use Session or ViewState variables:
Session
ViewState
As I said this is for asp.net but it might help anyways.
Edit: I still think the data is somewhere lost in this d-array... What if you just leave out the array and have your button_Click event like this:
String temp = Label1.Text;
Label1.Text = Label2.Text;
Label2.Text = Label3.Text;
Label3.Text = Label4.Text;
Label4.Text = Label5.Text;
Label5.Text = temp;
Your array named d (is there a better name for this?) is being re-declared on every page-load (as it seems like it should be).
The problem is that you're not persisting the adjusted array values each page load. Instead, you're starting from scratch each time as others have suggested.
See the following URL for a lot of useful information about persisting state information in a web-forms application.
http://msdn.microsoft.com/en-us/library/vstudio/z1hkazw7(v=vs.100).aspx
As far as your code, you can try something along these lines to persist the values from the current array while setting it up for the next load.
Public Class WebForm2 Inherits System.Web.UI.Page
Private _dValues As Integer(19)
Private _currentValues As Integer(19)
Private _newValues As Integer(19)
Private _startIndex As Integer
Public Sub New()
_dValues = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
_startIndex = 0
End Sub
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
If Page.IsPostBack Then 'Load values from form
For i As Integer = 0 To _currentValues.Length - 1 Step 1
_currentValues(i) = Request.Form(String.Concat("Hidden", i))
Next
_startIndex = Request.Form("StartIndex")
Else
_currentValues = _dValues 'First time around
End If
Dim position As Integer = _startIndex
For i As Integer = 0 To _currentvalues.Length - 1 Step 1
If position >= _newValues.Length Then
position = 0
End If
'Assign the current position in the new array equal to the current sequential value in the previous array
_newValues(position) = _currentValues(i)
position += 1
Next
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Increment the counter every post back
If Page.IsPostBack Then
_startIndex += 1
End If
'Don't allow the counter to go outside the bounds of the array
If _startIndex >= _currentValues.Length Then
_startIndex = 0
End If
Me.StartIndex.Text = _startIndex 'Assign the value of the hidden field
End Sub
Public Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
'Dynamically set label / hidden data
For i As Integer = 0 To _newValues.Length - 1 Step 1
CType(Page.FindControl(String.Concat("Label", i), Label).Text = _currentValues(i)
CType(Page.FindControl(String.Concat("Hidden", i), Hidden).Text = _newValues(i)
Next
End Sub
Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
'Dynamically set label / hidden data
For i As Integer = 0 To _newValues.Length - 1 Step 1
CType(Page.FindControl(String.Concat("Label", i), Label).Text = _currentValues(i)
CType(Page.FindControl(String.Concat("Hidden", i), Hidden).Text = _newValues(i)
Next
End Sub
End Class
Then in your form:
<asp:Hidden ID="StartIndex" runat="server" Value="" />
<asp:Label ID="Label1" runat="server" Value="" />
<asp:Hidden ID="Hidden1" runat="server" Value="" />
<asp:Label ID="Label2" runat="server" Value="" />
<asp:Hidden ID="Hidden2" runat="server" Value="" />
etc...
This code (or at least a version of it) will build a number of label and hidden input fields that will be used to display data and persist data. Each time the page is posted, the start index will be incremented, which will change the start position of the new values. While the current values will be based on what is posted back from the hidden inputs.
Am trying to print barcode labels, but when the label come out the printer the barcode printed in the top(need in the bottom), how can i control the coordinates of the barcode?
my code is
Private Sub printdc128_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim bc128 As New BarcodeLib.Barcode.Linear()
bc128.Data = TextBox2.Text
bc128.BarHeight = 60.0
bc128.BarWidth = 3.0
bc128.Resolution = 124
bc128.drawBarcode(e.Graphics)
End Sub
Private Sub printdc128_PrintPage(ByVal sender As System.Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs)
Dim img As System.Drawing.Image
Dim bc128 As New BarcodeLib.Barcode.Linear()
' Insert text
e.Graphics.DrawString(TextBox1.Text, New System.Drawing.Font("Arial", 16.0F), System.Drawing.Brushes.Black, New System.Drawing.PointF(10.0F, 10.0F))
' Select Code Type
bc128.Type = BarcodeLib.Barcode.BarcodeType.CODE128
' Select data source
bc128.Data = TextBox2.Text
' Choose Unit of Measures
bc128.UOM = BarcodeLib.Barcode.UnitOfMeasure.PIXEL
' Barcode Height and Width
bc128.BarHeight = 40.0
bc128.BarWidth = 2.0
' Convert text to stream
Dim bc128stream As New MemoryStream()
bc128.drawBarcode(bc128stream)
' Convert Stream to image
img = Drawing.Image.FromStream(bc128stream)
' Draw the image and Choose the coordinates(X,Y)
e.Graphics.DrawImage(img, New Drawing.PointF(10, 90))
End Sub
I have two questions.
How do I construct my XML- file which i want to output in a Stacked Column chart using the code-behind?
And secondly,
How do I put it in the code-behind in vb?
This is the code-behind at the moment:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Chart1.Width = 200
Chart1.Height = 300
Dim XMLFile As XElement = XElement.Load(Server.MapPath("~/xml/XMLFile.xml"))
Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.StackedColumn
Chart1.Series(0).Palette = DataVisualization.Charting.ChartColorPalette.BrightPastel
Chart1.Series("Series1").IsValueShownAsLabel = True
For Each node As XElement In XMLFile.Elements("something")
Dim string1 As String = node.Element("whatever1").Value
Dim string2 As String = node.Element("whatever2").Value
Chart1.Series("Series1").Points.AddXY(string1, string2)
Chart1.ChartAreas("ChartArea1").Area3DStyle.Enable3D = True
Chart1.ChartAreas("ChartArea1").Area3DStyle.Inclination = 15
Chart1.ChartAreas("ChartArea1").Area3DStyle.Rotation = 15
Next
End Sub
The XMLFile looks something like this at the moment:
<root>
<something>
<whatever1>One</whatever1>
<whatever2>1</whatever2>
</something>
<something>
<whatever1>Two</whatever1>
<whatever2>2</whatever2>
</something>
</root>
I need two columns with three different values stacked in each column.
How do I do that?