Interactive chart in C# - set cursor by mouse-click - c#

I'm new to C# and windowsforms applications. I'm trying to create a, at least to some extend, interactive chart. As of now I have not found a library or a built-in method to do what I have in mind.
I want to be able to:
Set a cursor when I click in the chart, set a 2nd cursor when I click again.
When 2 cursors are set, I want to be able to move them with the mouse (click to grab).
(If possible side by side with cursor click-placing:
"Zoom the chart by drawing a rectangle in the chart by clicking and moving the mouse".
My intention is to enable zoom as in: MS Chart Control: prevent zoom when clicking)
I'd like to do this to analyze data (see: Cursor setting example.png). With the cursors it should be possible to easily get the values of the two cursor positions (yellow and red lines) and to measure the distance in between (purple line).
Does anyone of you know how to do this with a mouse_click event OR know a chart library to easily do that?
Thanks for your help!
-Phill

Found another solution to my question and posted it as an answer there: How to retrieve the selected range in the .Net WinForms Chart Control?
To add colored cursors I also add 2 dataseries that are yellow and red to the method private void chart1_SelectionRangeChanging(object sender, CursorEventArgs e):
chart1.Series["CursorX1"].Points.AddXY(x1,y1Min);`
chart1.Series["CursorX1"].Points.AddXY(x1,y1Max);
chart1.Series["CursorX1"].Points.Color = Color.Red;
chart1.Series["CursorX2"].Points.AddXY(x2,y2Min);`
chart1.Series["CursorX2"].Points.AddXY(x2,y2Max);
chart1.Series["CursorX2"].Points.Color = Color.Yellow;

Related

How to make graphics (Shape, animation) in c#

How to make a circle with text inside ?? then move it from one location to another, and then access it later (to delete it).
I want to make something like this
Your question is really very broad and you got a few nice links you should study to learn all about GDI+ drawing.
But if taken literally there is a slightly exotic alternative which puts the burdon of most chores onto the Chart control from DataVisualization.Charting.
You can create EllipseAnnotations and add them to a Chart control.
Disable the Axes and clear the Legends and then use code like this to add a moveable circle wit thext inside:
EllipseAnnotation ea = new EllipseAnnotation();
ea.X = 11; // put at..
ea.Y = 11; // 11% of the chart's area
ea.AllowMoving = true;
ea.BackColor = Color.BlanchedAlmond;
ea.Text = (chart1.Annotations.Count + 1) + "";
chart1.Annotations.Add(ea);
Note that there are quite a few annotation types available. which allow you to add Rectangles, Images, Polygons, Lines and pure Text.
And another pro is that saving or loading the graphics takes only one line each, as you can serialize a Chart out of the box!
:-)
GraphX for .NET is an advanced open-source graph layout and visualization library that supports different layout algorithms and provides many means for visual customizations It is capable of rendering large amount of vertices
https://github.com/panthernet/GraphX
To draw shapes follow here.Also you need a complete tut,you can follow here
Some insight is here:
To draw a simple shape at design time Drag the OvalShape or
RectangleShape control from the Visual Basic PowerPacks tab (to
install, see Visual Basic Power Packs Controls)in the Toolbox to a
form or container control.
Drag the sizing and move handles to size and position the shape. You
can also size and position the shape by changing the Size and Position
properties in the Properties window To create a rectangle with rounded
corners, select the CornerRadius property in the Properties window
and set it to a value that is greater than 0. In the Properties
window, optionally set additional properties to change the appearance
of the shape. To draw a simple shape at run time On the Project
menu, click Add Reference. In the Add Reference dialog box, select
Microsoft.VisualBasic.PowerPacks.VS, and then click OK. In the Code
Editor, add an Imports or using statement at the top of the module:
using Microsoft.VisualBasic.PowerPacks; Add the following code in an Event procedure:
ShapeContainer canvas = new ShapeContainer();
// To draw an oval, substitute
// OvalShape for RectangleShape.
RectangleShape theShape = new RectangleShape();
// Set the form as the parent of the ShapeContainer.
canvas.Parent = this;
// Set the ShapeContainer as the parent of the Shape.
theShape.Parent = canvas;
// Set the size of the shape.
theShape.Size = new System.Drawing.Size(200, 300);
// Set the location of the shape.
theShape.Location = new System.Drawing.Point(100, 100);
// To draw a rounded rectangle, add the following code:
theShape.CornerRadius = 12;
Customizing Shapes When you use the default settings, the OvalShape and RectangleShape controls are
displayed with a solid black border that is one pixel wide and a
transparent background. You can change the width, style, and color of
the border by setting properties. Additional properties enable you to
change the background of a shape to a solid color, a pattern, a
gradient fill, or an image. Before you change the background of a
shape, you should know how several of the properties interact. The
BackColor property setting has no effect unless the BackStyle property
is set to Opaque. If the FillStyle property is set to Solid, the
FillColor overrides the BackColor. If the FillStyle property is set to
a pattern value such as Horizontal or Vertical, the pattern will be
displayed in the FillColor. The background will be displayed in the
BackColor, provided that the BackStyle property is set to Opaque. In
order to display a gradient fill, the FillStyle property must be set
to Solid and the FillGradientStyle property must be set to a value
other than None. Setting the BackgroundImage property to an image
overrides all other background settings.
This SO link I found is also nice here

New textBox and Label obscuring my zedGraph

I have a c# form which is to allow the user to specify a differential equation (dy/dt = -lambda*y) to solve both exactly and approximately (by entering desired values of the intital condition, time step and lambda into textBoxes). Clicking a button calculates the solutions and displays them numerically in boxes as they change over time (using a timer). When the timer finishes, 'Simulation Completed' is displayed in a MessageBox.
At this point, clicking the 'draw graph' button invokes zedGraph to graph the exact and approximate solutions. There are no problems wih calculating and graphing the solutions. The problem is that the label and textBox for timeStep (which I added after adding the zedGraph section) and the 'draw graph' button are superimposed on the graph, partially obscuring it. The textBoxes and labels for lambda and the initial condition were added to the program before the zedGraph part and don't get superimposed.
Is there a way to stop the timeStep label and textBox from being superimposed without having to write the program again, adding the textBox before the zedGraph section?
To summarise: the order in which you add things (at least the way I've done it, if not in general) determines wha happens: adding a textBox before adding the zedGraph section means it doesn't get superimposed on the graph. Add a textBox after adding the zedGraph section and it does get superimposed on the graph. I'm looking for a way o be able to add extra features, having already added the zedGraph section, without them being superimposed on the graph.
You can view 3 screenshots, 2 from before the problem was solved and 1 after here:
https://www.facebook.com/photo.php?fbid=10201376481749572&set=a.10201375193157358.1073741826.1099868090&type=3&theaterset=a.10201375193157358.1073741826.1099868090&type=3&theater
This is a screenshot of the problem:
I just worked out the answer (thanks to God): I just hide the objects I don't want to appear on the graph using the Hide() method at the start of my createGraph() method as follows:
private void CreateGraph(ZedGraphControl zgc)
{
textBox3.Hide();
textBox4.Hide();
label3.Hide();
label5.Hide();
button2.Hide();
Thanks everyone for your input. Quite easy in the end, thankfully... what a relief.
Here's a link to the finished graph:
https://www.facebook.com/photo.php?fbid=10201375420003029&set=a.10201375193157358.1073741826.1099868090&type=3&theater

Simulate Mouse Drag Programmatically

I have a Windows Forms application with a control. The control consists of a chart panel with a canvas on which I paint. What I would like to do is to programmatically mouse drag the panel so that I have a specific distance between the right edge of the canvas and the last item painted on the canvas. I have tried two approaches. The both work in the sense that the panel is dragged as desired BUT I cannot seem to be able to get the precision of movement I desire. I coded a mouse simulator and have tried two approaches.
Approach 1:
if(this.ChartControl.ChartPanel.CanFocus)
{
// ... Focus the chart panel to be adjusted.
this.ChartControl.ChartPanel.Focus();
// ... Move cursor to lastBarScreenCoordinates on the chart panel to be adjusted.
Cursor.Position = new Point(lastBarScreenCoordinates.X, lastBarScreenCoordinates.Y);
MouseSimulator.SetMouseDragThresholds();
// ... Move chart panel to required position.
MouseSimulator.LeftMouseButtonDown(lastBarScreenCoordinates.X, lastBarScreenCoordinates.Y);
MouseSimulator.MouseMove(lastBarScreenCoordinates.X-positionShift,
lastBarScreenCoordinates.Y);
MouseSimulator.LeftMouseButtonUp(lastBarScreenCoordinates.X-positionShift,
lastBarScreenCoordinates.Y);
MouseSimulator.ResetMouseDragThresholds(_cx_default, _cy_default);
// ... Redraw the chart panel.
this.ChartControl.ChartPanel.Refresh();
// ... Reset cursor to its starting position.
Cursor.Position = new Point(startingCursorX, startingCursorY);
}
Approach 2:
if(this.ChartControl.ChartPanel.CanFocus)
{
// ... Focus the chart panel to be adjusted.
this.ChartControl.ChartPanel.Focus();
// ... Move cursor to lastBarScreenCoordinates on the chart panel to be adjusted.
Cursor.Position = new Point(lastBarScreenCoordinates.X, lastBarScreenCoordinates.Y);
MouseSimulator.SetMouseDragThresholds();
// ... Move chart panel to required position.
MouseSimulator.LeftMouseButtonDown(lastBarScreenCoordinates.X, lastBarScreenCoordinates.Y);
Cursor.Position = new Point(lastBarScreenCoordinates.X-positionShift,
lastBarScreenCoordinates.Y);
WindowsCommunication.SendMessage(this.ChartControl.Handle, 0x200, IntPtr.Zero,IntPtr.Zero);
MouseSimulator.LeftMouseButtonUp(lastBarScreenCoordinates.X-positionShift,
lastBarScreenCoordinates.Y);
MouseSimulator.ResetMouseDragThresholds(_cx_default, _cy_default);
// ... Redraw the chart panel.
this.ChartControl.ChartPanel.Refresh();
// ... Reset cursor to its starting position.
Cursor.Position = new Point(startingCursorX, startingCursorY);
}
I am using SendInput for simulating mouse clicks. Here is sample left mouse button down code ...
public static void LeftMouseButtonDown(int x, int y)
{
INPUT mouseInput = new INPUT();
mouseInput.type = SendInputEventType.InputMouse;
mouseInput.mkhi.mi.dx = CalculateAbsoluteCoordinateX(x);
mouseInput.mkhi.mi.dy = CalculateAbsoluteCoordinateY(y);
mouseInput.mkhi.mi.mouseData = 0;
mouseInput.mkhi.mi.time = 0;
mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTDOWN;
SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
}
And I calculate normalized absolute coordinates for the mouse as follows ...
private static int CalculateAbsoluteCoordinateX(int x)
{
return ((x * 65536) + GetSystemMetrics(SystemMetric.SM_CXSCREEN) - 1) /
GetSystemMetrics(SystemMetric.SM_CXSCREEN);
}
So here are the precision issues. If I use Approach 1 (mouse move), the measured distance between the last item painted and the right edge of the canvas is different from what I set in positionShift and the cursor position difference does not equal positionShift. I initially thought it was due to pointer ballistics issues so I tried using Approach 2. Approach 2 does give me precision in pointer positioning but I am still having difficulty in that the panel moves but the distance between the last bar painted and the right edge of the canvas does not equal the positionShift amount as it should. It always seems to be off. I have been working on this for a long time now and am at my wits end. I am not sure what is going on here. How to improve the precision in my canvas drag by simulated mouse drag?
Well what you can do is this, First of all I believe the SendInput API allows for an AbsoluteValue flag so there is no need to calculate those values which may be the issue but most likely not.
Although I am curious as to why you are using a Mouse Drag opperation for this. It seems like all you want to do is reposition the canvas on every draw by some specified amount. if this is the case why not just set it explicitly on the canvas itself. Also it is unclear if you are using pure WinForms or WPF. The unclear bit being Canvas which I am fairly certain is only usable with WPF enabled windows.
That being said
WPF fix,
Depending on the Object containing the canvas just set its margin appropriately for the situation, since I do not know the data you are working with I cant say much about that. But this is a relatively simple idea so let me know if that works, it should give you, at least close too, a pixel perfect alignment.
WinForms,
Just do the above for the "Canvas" object you were talking about, or use absolute coordinates of the object to move it around.
If you could supply a sample of what you were working on looked like roughly maybe we could have a better idea of what you mean.

Can I select points in a polar chart in MSChart?

I would like to select points in a Polar chart in MSChart.
I have the ChartAreas.CursorX(and Y).IsUserSelection = true. But when I try to select a zone, the SelectionChanged event does not activate, nor do I see a selection in the chart.
No, looking at the inner code of mschart, the cursors user selection is inhibited when the chart area is circular (as in the polar chart).
In fact the decompiled code of chart.MouseDown is something like this:
if(!area.IsCircular ...)
{
area.CursorX.Cursor_MouseDown(this, e);
area.CursorY.Cursor_MouseDown(this, e);
}
So, the only way is to handle the MouseClick/MouseMove events, get the points values using HitTest method and do whatever you need manually.
For example, this answer explains how to show a tooltip on MouseClick/MouseMove event.
EDIT :
Here's a full working code piece showing how to implement the selection in a polar chart.
Screen-shot:

how to change the direction of X-axis label in ms charts

Hi I am using Ms chart control in winforms application for displaying values according to dates
I need to change the x-axis label values(Dates) direction horizantal to vertical
I have searched so many properties but i did not find any solution for this.
Any one help me on this problem
Many Thanks ....
As I understand your question - you are asking how to rotate the chart label to display vertically.
You can rotate the x-axis label as follows:
chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -90;
This assumes you have associated your series with the first chart area, which is the default without modification when using the Winforms designer.
The following images shows how the chart would look before the code above is applied, the second image shows how it appears after the code is applied.
Let me know if this is not what you are trying to do and I will post an updated answer.
Before rotation
After Rotation
Edit: Another answer added after my initial post mentions in certain situations it may be important to set chartArea1.AxisX.IsLabelAutoFit = false;
If you have not already done so, get the chart samples from microsoft:
http://archive.msdn.microsoft.com/mschart
Then check the section on Labels
Chart Features > Labels
To answer your question directly, set the angle in LabelStyle, and don't forget to disable autofit
chartArea1.AxisX.IsLabelAutoFit = false;
chartArea1.AxisX.LabelStyle.Angle = 90;

Categories