Why does auto Zoom/Scroll not work for my Chart? - c#

to make it short I checked on the "WinFormsChartSamples" provided by Microsoft. What I wanted to know is how to enable zooming and scrolling for Chartcontrols. The example which is shown there is pretty short.
using System.Windows.Forms.DataVisualization.Charting;
...
// Set automatic zooming
chart1.ChartAreas["Default"].AxisX.ScaleView.Zoomable = true;
chart1.ChartAreas["Default"].AxisY.ScaleView.Zoomable = true;
// Set automatic scrolling
chart1.ChartAreas["Default"].CursorX.AutoScroll = true;
chart1.ChartAreas["Default"].CursorY.AutoScroll = true;
...
I tried this and nothing happened, no zooming and no scrolling. I tried two things:
In Form1.Designer.cs I added that information to the chart.
chartArea1.Name = "ChartArea1";
chartArea1.CursorX.AutoScroll = true;
chartArea1.CursorY.AutoScroll = true;
chartArea1.AxisX.ScaleView.Zoomable = true;
chartArea1.AxisY.ScaleView.Zoomable = true;
this.chart1.ChartAreas.Add(chartArea1);
this.chart1.Cursor = System.Windows.Forms.Cursors.Cross;
legend1.Name = "Legend1";
this.chart1.Legends.Add(legend1);
this.chart1.Location = new System.Drawing.Point(297, 62);
this.chart1.Name = "chart1";
series1.ChartArea = "ChartArea1";
series1.Legend = "Legend1";
series1.Name = "Series1";
this.chart1.Series.Add(series1);
this.chart1.Size = new System.Drawing.Size(963, 668);
this.chart1.TabIndex = 6;
this.chart1.Text = "chart1";
I tried to add it directly into the constructor in Form1.cs.
Perhaps it is important to mention that I am using OpenFileDialog in order to add data to the series:
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
Stream fileStream = null;
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Open File..";
//First the description of the file separated by "|"
fDialog.Filter = "((ASC files)| *.asc";
fDialog.InitialDirectory = #"C:\";
//Show Messagebox if the file was loaded (Source: MSDN - FileDialog.FilterProperty)
if (fDialog.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("The File was loaded successfully.");
try
{
if ((fileStream = fDialog.OpenFile()) != null)
{
using (fileStream)
{
//Insert code for reading the stream here.
Spectrum newSpectrum = new Spectrum(chart1.Series.Count, fDialog.FileName,
fDialog.SafeFileName, DataHandler.readSpectrumFromFile(fileStream));
addSpectrumToView(newSpectrum);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
Any advice is welcome, thanks in advance,
BC++

I think you were actually really looking for this:
chart1.ChartAreas["Default"].CursorX.IsUserSelectionEnabled = true;
chart1.ChartAreas["Default"].CursorY.IsUserSelectionEnabled = true;
used in conjunction with what you already have should work well, which should look like this:
// Set automatic zooming
chart1.ChartAreas["Default"].AxisX.ScaleView.Zoomable = true;
chart1.ChartAreas["Default"].AxisY.ScaleView.Zoomable = true;
// Set automatic scrolling
chart1.ChartAreas["Default"].CursorX.AutoScroll = true;
chart1.ChartAreas["Default"].CursorY.AutoScroll = true;
// Allow user selection for Zoom
chart1.ChartAreas["Default"].CursorX.IsUserSelectionEnabled = true;
chart1.ChartAreas["Default"].CursorY.IsUserSelectionEnabled = true;

Have a look here: http://archive.msdn.microsoft.com/mschart There is an example there which does zooming/scrolling and much, much more! :)

To enable easy zooming, add a trackbar and use it to zoom:
private void trackBar1_Scroll(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.ScaleView.Size = trackBar1.Maximum - trackBar1.Value;
chart1.ChartAreas[1].AxisX.ScaleView.Size = trackBar1.Maximum - trackBar1.Value;
(etc for however many chart areas you have)
}
the "maximium - value" is to so that the higher the trackbar value, the fewer points are shown (closer zoom)
and make sure that in designer the 'chart1->ChartAreas->Axes->(whichever axes)->scaleview->zoomable' is set to true
A scroll bar will normally appear when a datapoint exceeds the scaleview size of an axis, if it has been set (scrolling doesn't really work reliably if left at 'auto'), if it hasn't, set it, if a scrollbar doesn't appear, a trackbar can yet again be used:
private void trackBar2_ValueChanged(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.ScaleView.Position = trackBar2.Value;
chart1.ChartAreas[1].AxisX.ScaleView.Position = trackBar2.Value;
(etc for however many chart areas you have)
}
Make sure you set the "Maximum" in the trackbars to a nice high number (eg 5000) and "Value" to what you desire it to load at.
Have yet to notice too much of a difference between "trackBar_Scroll" and "trackBar_ValueChanged", except "ValueChanged" works if the trackbar is moved by the program or user mouse click, whereas "Scoll" only works if moved by users mouse click.
Anything I missed?

My users dislikes the standard behavior of the mschart zooming and scrolling. That's why I implement a mouse based zoom/scroll that use dragging and mousewheel on axises.
The source code is here: https://bitbucket.org/controlbreak/mschartfriend
It is very simple and short, you can change it very quickly if you need.

Related

Windows Presentation Foundation Print Label (Zebra Printer)

I am trying to print text to display vertical in Windows Forms Host. The label is printing with report viewer in WPF. Here is my code:
// boolean is based on true or false, when printing labels
private bool _isReportViewerLoaded;
// method to display data in .rdlc
private void ReportViewer_Load(object sender, EventArgs e)
{
// if equal false run this code isReportViewerLoaded
if (!_isReportViewerLoaded)
{
// get the lot based on the parameter Id
Lot lot = BottleLotRespository.GetLotById(this.Parameter);
// settings the page settings
PageSettings pg = new PageSettings();
pg.PrinterSettings
.DefaultPageSettings
.Margins = new Margins(0, 0, 0, 0);
pg.Landscape = false;
PaperSize size = new PaperSize("110.0 x 74.0", 433, 100);
BottleLotDataSet bottleLotDataSet = new BottleLotDataSet();
DataTable reportDataTable = bottleLotDataSet.LotDataTable;
DataRow lotRow = reportDataTable.NewRow();
lotRow["Id"] = lot.Id;
lotRow["Number"] = lot.Number.ToString();
reportDataTable.Rows
.Add(lotRow);
bottleLotDataSet.BeginInit();
this._reportViewer.SetPageSettings(pg);
this.reportDataSource.Name = "DataSet1";
this.reportDataSource.Value = reportDataTable;
this._reportViewer
.LocalReport
.DataSources
.Add(this.reportDataSource);
this._reportViewer
.LocalReport
.ReportEmbeddedResource = "BottleLotWPF.View.Report1.rdlc";
bottleLotDataSet.EndInit();
_reportViewer.RefreshReport();
_isReportViewerLoaded = true;
}
}
My problem is that the Report1.rdlc is not allowing me to rotate the text and there is no settings for it. Is there away of adding a setting to it to rotate the text?
In RDLC we have the option to print vertically it seems.
please take look at the existing thread here and see if it helps.
display-text-vertically-start-

Label without Padding and Margin

I have the following code in C# (.NET Framework 3.5)
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
Label myControl = new Label();
myControl.Text = "TEXT";
myControl.FlatStyle = FlatStyle.System;
myControl.AutoSize = true;
myControl.BorderStyle = BorderStyle.FixedSingle;
myControl.Padding = new Padding(0);
myControl.Margin = new Padding(0);
this.Controls.Add(myControl);
InitializeComponent();
}
}
Which should display a label with the text enclose by a border, like this:
------
|TEXT|
------
Instead, I get this:
--------
|TEXT |
--------
And I don't know why... My objective is to be able to have multiple labels without space between them, like this:
-----------
|TEXT|TEXT|
-----------
Am I missing something? Thanks in advance!
For clarification, I need to have NO SPACE between the text and the border.
This is what solved it for me (using #LarsTech's solution):
I added
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated(e);
this.AutoSize = false;
}
protected override void OnFontChanged(EventArgs e) {
base.OnFontChanged(e);
this.Size = GetTextSize();
}
protected override void OnResize(EventArgs e) {
base.OnResize(e);
this.Size = GetTextSize();
}
protected override void OnTextChanged(EventArgs e) {
base.OnTextChanged(e);
this.Size = GetTextSize();
}
private Size GetTextSize() {
Size padSize = TextRenderer.MeasureText(".", this.Font);
Size textSize = TextRenderer.MeasureText(this.Text + ".", this.Font);
return new Size(textSize.Width - padSize.Width, textSize.Height);
}
to my label definition.
I also added
textLabel.FlatStyle = FlatStyle.System;
Thank you very much for the help!
I don't know what's going on with the FlatStyle property, except to say that FlatStyle.System has a similar effect on my system. The other FlatStyle values indicate clearly what the effect will be on the control, but FlatStyle.System is pretty nebulous.
The appearance of the control is determined by the user's operating system.
I'm not sure what in the OS plays a role in the layout of he control. LarsTech's comment about changing it to FlatStyle.Standard (or any other value for that matter) fixes the issue for me (and doesn't trim off any text, as your comment indicates is happening to you).
You can override the alignment behavior by explicitly setting it to the center:
myControl.TextAlign = ContentAlignment.MiddleCenter;
I'm not sure exactly what you're trying to achieve (since it seems you could just enter all of your text in a single Label, not multiple next to each other), but you may also want to remove the border style:
myControl.BorderStyle = BorderStyle.None;
And, similar to what Blablablaster said, consider using a FlowLayoutPanel and adding your Label controls to that. You can place the above code in a loop, adding each one to the panel, and it'll take care of laying them out next to each other for you.
for (var i = 0; i < 10; i++)
{
Label myControl = new Label();
myControl.Text = "TEXT";
...
...
flowLayoutPanel1.Controls.Add(myControl);
}

FlowLayoutPanel AutoSize only in vertical?

I'm loading images dynamically inside a FlowLayoutPanel. I need for this panel to auto-size but only vertically.
Is this possible, and if so, how do I go about achieving it?
Simple, add a event of type control added:
private void flowLayoutPanel1_ControlAdded(object sender, ControlEventArgs e)
{
if (flowLayoutPanel1.Controls.Count % 10 == 0)
flowLayoutPanel1.SetFlowBreak(e.Control as Control, true);
}
set AutoSize = true
set flowdirection = LeftToRight
Maybe
FlowLayoutPanel1.WrapContents = False;
FlowLayoutPanel1.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
will help you.
I did set the Size from panel dinamically. Example:
int newHeight= listImages.Count/10 * 100;
flowLayoutPanel1.Size = new Size(1143, newHeight);
It works for me. Thx all
This might look like an ugly solution, but it works for me:
Store current width of a panel in variable;
Set AutoSize mode to true;
Perform the action that require panel resize;
Restore previous panel's width from the variable.
int i = _panel1.Width;
_panel1.AutoSize = true;
_panel1.AutoSizeMode = AutoSizeMode.GrowOnly;
/*some action going on here*/
_panel1.AutoSize = false;
_panel1.Size = new Size(_panel1.Width, 80);

AutoScrollPosition Always Returns (0,0) For SplitPanel Control

I am trying to syncronize the scrolling of two splitcontainers within a splitpanel control. I have the code below:
Point mPrevPan1Pos = new Point();
Point mPrevPan2Pos = new Point();
void PanelPaint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if (splitContainer1.Panel1.AutoScrollPosition != mPrevPan1Pos)
{
splitContainer1.Panel2.AutoScrollPosition = new System.Drawing.Point(-splitContainer1.Panel1.AutoScrollPosition.X, -splitContainer1.Panel1.AutoScrollPosition.Y);
mPrevPan1Pos = splitContainer1.Panel1.AutoScrollPosition;
}
else if (splitContainer1.Panel2.AutoScrollPosition != mPrevPan2Pos)
{
splitContainer1.Panel1.AutoScrollPosition = new System.Drawing.Point(-splitContainer1.Panel2.AutoScrollPosition.X, -splitContainer1.Panel2.AutoScrollPosition.Y);
mPrevPan2Pos = splitContainer1.Panel2.AutoScrollPosition;
}
}
However the AutoScrollPosition is always (0,0). I have AutoScroll enabled for both split containers. Why is this? What can I do to get the scroll position?
It looks like you copied the code from this answer: Scroll 2 panels at the same time
Did you wire up the events:
this.splitContainer1.Panel1.Paint += new PaintEventHandler(PanelPaint);
this.splitContainer1.Panel2.Paint += new PaintEventHandler(PanelPaint);

Troubles with setting ToolTip duration programmatically

I've come across a situtation where I need to create a tooltip object and show it when the user hovers over specific areas in my application.
I can get the tooltip to show up just fine. The problem is I need it to go poof after a few seconds have passed. I'm aware of the ToolTipService.SetShowDuration and I've tried using it, but I have not been met with much luck.
Here's what I got in my MouseMove event handler:
_toolTip.Placement = PlacementMode.Relative;
_toolTip.Horizontal = e.X;
_toolTip.VerticalOffset = e.Y;
_toolTip.Content = stuffs;
_toolTip.IsOpen = true;
I've tried setting the following:
someObject.ToolTip = _toolTip;
ToolTipService.SetShowDuration(someObject, 5);
Nothing changes with the last two lines. The tooltip still is visible and stays visible. Am I using the service wrong or something? Any thoughts would be much appreciated!
Try this.
<Border Name="border" ToolTip="some message" MouseEnter="border_MouseEnter" Background="red" Margin="50"/>
ToolTip tool = new ToolTip();
private void border_MouseEnter(object sender, MouseEventArgs e)
{
tool.Placement = PlacementMode.Relative;
tool.HorizontalOffset = 100;
tool.VerticalOffset = 200;
tool.Content = "stuffs";
tool.IsOpen = true;
border.ToolTip = tool;
ToolTipService.SetShowDuration(border, 5000);
}
I developed a workaround for the issue.
To give a little more background, I have a 3D model of an aircraft inside of a WindowsFormsHost object. When the user hovers over a identified part, I needed a tooltip to appear.
I created an instance of tooltip and in my MouseMove event and I do something like this:
// selectedPart will be null if no part is selected
if(selectedPart != null && prevSelectedPart != selectedPart)
{
toolTip.IsOpen = false;
host.ToolTip = toolTip;
toolTip.IsOpen = true;
}
else if (prevSelectedPart == selectedPart && prevSelectedPart != null)
{
toolTip.IsOpen = true;
}
else
toolTip.IsOpen = false;
That does the trick for me.

Categories

Resources