2014年7月13日 星期日

[RESOLVED] Help with point graph


Hi


I am trying to create a coordinates graph using point chart, pulling the data from the MS sql server. 


I want help with the following:


1. How can I change the colour of the point on the chart for my latest data entry?


2. How title my axis as N, S, E & W?  I can only get as shown in figure.


Chart1.ChartAreas(0).AxisX.Title = "W"
Chart1.ChartAreas(0).AxisY.Title = "S"




3. Also, can I the coordinates value just buy hovering over the points on the graph?





Or is there a better way of creating a coordinate graph?





Thanks a lot in advance


Harkirat




sorry i dont know how to attach an image in the forum. :(



Hi harkirat,


Please try to refer to the following solution:



  1. Please try to according to your data, and using Chart1.Series["Series1"].Points[pointIndex].Color = System.Drawing.Color.Red; to update color;
  2. I think the chart only supports two dimensions;
  3. Please try to using ToolTip to achieve the mouse hovering.

The following code is my sample, please try to refer it:


In the aspx file:













In the aspx.cs file:


private void FillData()
{
double plotY = 50.0;
if (Chart1.Series["Series1"].Points.Count > 0)
{
plotY = Chart1.Series["Series1"].Points[Chart1.Series["Series1"].Points.Count - 1].YValues[0];

}

Random random = new Random();
for (int pointIndex = 0; pointIndex < 20; pointIndex++)
{
plotY = plotY + (float)(random.NextDouble() * 10.0 - 5.0);
Chart1.Series["Series1"].Points.AddY(plotY);
Chart1.Series["Series1"].Points[pointIndex].ToolTip = plotY.ToString();
if (plotY > 40)// you can also according to data to change the color.
{
Chart1.Series["Series1"].Points[pointIndex].Color = System.Drawing.Color.Red;

                }
}
}

Hope it can help you.


If your problem still exists, please let me know.


Best Regards,

Terry Guo



Hi Terry


Thanks a million for your reply. Problems:


1. Sorted, thank you


2.Sad to hear about that, is there other type of chart I can use to get all 4 coordinate axis(x, -x,  y, -y)??


3. I cannot get the code to work. I am struggling a lot with it because I cannot interpret whats going on in there, small comments will be handy, thanks. ;)


This is how I am using with my existing code, can you please correct me.


Imports System.Web.UI.DataVisualization.Charting
Imports System.Data.OleDb
Imports System.Data.SqlClient

Public Class db_con
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
FillData()
Dim mySelectQuery As String = "SELECT TOP 5 test_x, test_y FROM test_x_y ORDER BY test_id DESC;"

Dim myConnection As New SqlConnection("Data Source= ONC-ECC54; Initial Catalog=graph_test; Integrated Security=True")
Try
' Open the connection
myConnection.Open()

' Create a database command on the connection using query
Dim myCommand As New SqlCommand(mySelectQuery, myConnection)

' Create a database reader
Dim myReader As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

Chart1.Series("Series1").Points.DataBindXY(myReader, "test_x", myReader, "test_y")

' Close the reader and the connection
myReader.Close()
myConnection.Close()

Catch Ex As Exception
Label1.Text = "Error in acquiring graph chart " & Ex.ToString
End Try


Chart1.Series(0).Color = Drawing.Color.RosyBrown
Chart1.Series(0).Points(0).Color = System.Drawing.Color.Red

Chart1.ChartAreas(0).AxisX.Minimum = -10
Chart1.ChartAreas(0).AxisX.Maximum = 10

Chart1.ChartAreas(0).AxisY.Minimum = -10
Chart1.ChartAreas(0).AxisY.Maximum = 10


End Sub

Private Sub FillData()
Dim plotY As Double = 50.0
If (Chart1.Series(0).Points.Count > 0) Then
plotY = Chart1.Series(0).Points(Chart1.Series(0).Points.Count - 1).YValues(0)
End If

Dim rnd1 As New Random()
For pointIndex As Integer = 0 To 20 Step 1
plotY = plotY + (rnd1.NextDouble() * 10.0 - 5.0)
Chart1.Series(0).Points.AddY(plotY)
Chart1.Series(0).Points(pointIndex).ToolTip = plotY.ToString
If plotY > 40 Then
Chart1.Series(0).Points(pointIndex).Color = Drawing.Color.AliceBlue
End If
Next
Debug.WriteLine("")
End Sub
End Class

in my aspx file:


















Thanks alot in advance


Harkirat


沒有留言:

張貼留言