Solved

C Sharp Chart - one axis with two titles, image attached

Posted on 2011-09-06
6
469 Views
Last Modified: 2012-05-12
Hi,

I am new to coding in c# and not very good. I basically have a simple line chart with which shows a stock price over the course of a couple of days.

On the y-axis I have the price. On the x-axis I have the date & time. However on the x-axis at every interval the date & time is shown but it is very hard to read what I would like is to be able to display the x-axis like the in the picture attached. I have no idea how to do this.

Any help would be great.

Thanks,

Mark


Chart-Pic2-Ex.doc
0
Comment
Question by:mcs26
  • 4
  • 2
6 Comments
 
LVL 14

Accepted Solution

by:
binaryevo earned 500 total points
ID: 36490133
Here is an example... Give that a try and let me know if that gets you where you need to be.

            List<TestObject> bonds = LoadTestObjects(this.dropPortfolios.SelectedValue.Trim()).ToList();

            if (bonds != null && bonds.Count > 0)
            {
                this.Chart1.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = true;
                this.Chart1.ChartAreas["ChartArea1"].AxisY.Title = "Duration";
                this.Chart1.ChartAreas["ChartArea1"].AxisX.Title = "Scenario";
                this.Chart1.ChartAreas["ChartArea1"].ShadowOffset = 2;

                this.Chart1.Legends["Default"].BackColor = System.Drawing.Color.White;
                this.Chart1.Legends["Default"].BorderColor = System.Drawing.Color.Black;
                this.Chart1.Legends["Default"].BorderWidth = 0;
                this.Chart1.Legends["Default"].Docking = System.Web.UI.DataVisualization.Charting.Docking.Bottom;
                this.Chart1.Legends["Default"].Alignment = System.Drawing.StringAlignment.Center;
                this.Chart1.Legends["Default"].LegendStyle = System.Web.UI.DataVisualization.Charting.LegendStyle.Column;
                this.Chart1.Legends.Add(new System.Web.UI.DataVisualization.Charting.Legend("MainLegend"));
                this.Chart1.Legends["Default"].IsEquallySpacedItems = true;


                foreach (Domain.VBond bond in bonds)
                {
                    System.Web.UI.DataVisualization.Charting.Series series = new System.Web.UI.DataVisualization.Charting.Series(bond.Sector);
                    series.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Line;
                    series.MarkerStyle = System.Web.UI.DataVisualization.Charting.MarkerStyle.Circle;
                    series.Points.AddY(Convert.ToDouble(bond.Number1));
                    series.Points.AddY(Convert.ToDouble(bond.Number2));
                    series.Points.AddY(Convert.ToDouble(bond.Number3));
                    series.Points.AddY(Convert.ToDouble(bond.Number4));
                    series.Points.AddY(Convert.ToDouble(bond.Number5));

                    series.Points[0].AxisLabel = "-200";
                    series.Points[1].AxisLabel = "-100";
                    series.Points[2].AxisLabel = "Base";
                    series.Points[3].AxisLabel = "+100";
                    series.Points[4].AxisLabel = "+200";
                    series.Legend = "MainLegend";
                    series.BorderWidth = 3;
                    this.Chart1.Series.Add(series);
                }

Open in new window

0
 

Author Comment

by:mcs26
ID: 36494295
Hi binaryevo,

Thanks for the reply. Will try the code as soon as I can, we'r having IT issues today in our office.

Here is the following basic code I have in the form class.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
string cn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
string dbPath = @"C:\Blah\";
string[] Pairs = new string[] { "AUDCAD", "AUDCHF", "AUDJPY", "AUDNZD", "AUDUSD", "CADJPY", "CHFJPY", "EURAUD", "EURCAD", "EURCHF", "EURDKK", "EURGBP", "EURJPY", "EURNOK", "EURNZD", "EURSEK", "EURUSD", "GBPAUD", "GBPCAD", "GBPCHF", "GBPJPY", "GBPNZD", "GBPUSD", "NZDJPY", "NZDUSD", "USDCAD", "USDCHF", "USDDKK", "USDHKD", "USDJPY", "USDNOK", "USDSEK", "USDSGD", "USDZAR" };
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int i;
// set the date & time pickers
this.dateTimePickerFrom.Format = DateTimePickerFormat.Custom;
this.dateTimePickerFrom.CustomFormat = "dd/MM/yyyy HH:mm:ss";
this.dateTimePickerTo.Format = DateTimePickerFormat.Custom;
this.dateTimePickerTo.CustomFormat = "dd/MM/yyyy HH:mm:ss";
// temp
this.dateTimePickerFrom.Value = new DateTime(2010, 1, 1);
this.dateTimePickerTo.Value = new DateTime(2010, 3, 31);
// set the combo box list & start it at EURUSD
for (i = 0; i < Pairs.Length; i++)
{
this.cmbPairs.Items.Add(Pairs[i]);
}
cmbPairs.Text = "EURUSD";
chartFXPair.ChartAreas.Clear();
chartFXPair.Series.Clear();
}
private void LoadDataPointIntoGraph(DateTime dtFrom, DateTime dtTo, string FXPair)
{
double minPrice = 999, maxPrice ; 
chartFXPair.ChartAreas.Clear();
chartFXPair.Series.Clear();
chartFXPair.Titles.Clear();
ChartArea cArea = new ChartArea("Area");
Series seriesPrice = new Series("PriceData");
chartFXPair.Titles.Add(FXPair);
seriesPrice.ChartType = SeriesChartType.Line;
chartFXPair.Series.Add(seriesPrice);
OleDbConnection dbConnection = new OleDbConnection(cn + dbPath + "dbFX_1Min_db.mdb");
dbConnection.Open();
OleDbCommand SQLQuery = dbConnection.CreateCommand();
SQLQuery.CommandText = @"SELECT [Date]+[Time] AS DateAndTime, 
Round(([Close_B]+[Close_A])/2,5) AS Close_Mid
FROM " + FXPair +
" WHERE ((([Date])>=#" + dtFrom.Date + @"# 
And ([Date])<=#" + dtTo.Date + @"#) 
AND (([Time])>=#12/30/1899 " + dtFrom.Hour + ":" + dtFrom.Minute + ":" + dtFrom.Second + @"# 
And ([Time])<=#12/30/1899 " + dtTo.Hour + ":" + dtTo.Minute + ":" + dtTo.Second + @"#))
ORDER BY [Date], [Time];"; 
OleDbDataReader PriceReader = SQLQuery.ExecuteReader();
if (PriceReader.HasRows)
{
while (PriceReader.Read())
{
if ((Double)PriceReader["Close_Mid"] > maxPrice)
maxPrice = (Double)PriceReader["Close_Mid"];
else if ((Double)PriceReader["Close_Mid"] < minPrice)
minPrice = (Double)PriceReader["Close_Mid"];
seriesPrice.Points.AddXY((DateTime)PriceReader["DateAndTime"], (Double)PriceReader["Close_Mid"]);
}
}
chartFXPair.ChartAreas.Add(cArea);
cArea.AxisY.Minimum = minPrice;
cArea.AxisY.Maximum = maxPrice;
PriceReader.Close();
dbConnection.Close();
}
private void buttPlot_Click(object sender, EventArgs e)
{
string txt;
txt = cmbPairs.SelectedItem.ToString() + "_1Min";
LoadDataPointIntoGraph(dateTimePickerFrom.Value, dateTimePickerTo.Value, txt);
}

Open in new window

0
 

Author Comment

by:mcs26
ID: 36494329
Just to be clear I've attached another picture as after re-reading my first post I do not think it was excatly clear as to what I want.

If you open the word document you will see a snap shot of a chart. On the x-axis you will see the time ie 12:00, 14:00. Beneath the time values you will see the actual date 06 Sep 2011. When the time value then moves past midnight the date value 07 Sep 2011 is displayed.

To me it appears there has to be two 'labels' for the x-axis to do this. If you carry scrolling on the chart as the time changes so does the date beneath the time values to the correct date. This is what I am trying to replicate.

Again thanks for any help!
Chart-Pic3-Ex.doc
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:mcs26
ID: 36494456
Think I may have found a soultion in the link below however still need to modify it for my code. Will try once our computers are up and running again.

http://www.dotnetcharting.com/documentation/v4_4/AxisTimeAutomation.html
0
 

Author Comment

by:mcs26
ID: 36502258
I've requested that this question be closed as follows:

Accepted answer: 0 points for mcs26's comment http:/Q_27293092.html#36494456

for the following reason:

The link provides an example of excatly what I was looking for. Code just needs to be modified for my program
0
 
LVL 14

Expert Comment

by:binaryevo
ID: 36502259
I think you should at least award partial points since the first post wasnt as clear...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now