private void GenerateSourceCodeGraph(int country_id, string xtitle, string ytitle, string pickedColumn, string typeofcalculation, string graphtype, string granularity, int start_year_no, int end_year_no, bool add, bool isFloat, bool addScaleY)
        {
            if (!add)
            {
                //rensar data o börjar på bygget
                sc.Clear();
            }

            string country_name = "";

            using (CarsDWEntities dw = new CarsDWEntities())
            {
                List <BigView> Context = dw.BigViews.Where(c => c.country_id == country_id).ToList();
                country_name = dw.Countries.Where(c => c.country_id == country_id).Select(a => a.name).FirstOrDefault();
            }

            sc.generateStaticContext(country_id.ToString(), add);
            if (granularity.ToLower() == "year")
            {
                sc.GenerateXYValuesYear(pickedColumn, start_year_no.ToString(), end_year_no.ToString(), isFloat);
            }
            else if (granularity.ToLower() == "month")
            {
                sc.GenerateXYValuesMonth(pickedColumn, start_year_no.ToString(), end_year_no.ToString(), isFloat);
            }
            sc.addSeries(country_name, pickedColumn, graphtype.ToLower(), xtitle, ytitle, addScaleY, add);

            sc.FinishUsing();
        }
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            int i = e.ProgressPercentage;

            using (CarsDWEntities dw = new CarsDWEntities())
            {
                double total    = (double)dw.BigViews.Where(a => a.country_id == 1 && a.year_no == current_year && a.month_no == i).Sum(b => b.total);
                double electric = (double)dw.BigViews.Where(a => a.country_id == 1 && a.year_no == current_year && a.month_no == i).Sum(b => b.electric);
                double totalcarsminuselectric = total - electric;

                //cvTemp2.Add(totalcarsminuselectric);
                elbilSveSeries.Values = new ChartValues <ObservableValue> {
                    new ObservableValue(electric)
                };
                //  otherCarsSveSeries.Values = cvTemp2;

                //tar hand om norge
                //double total = (double)dw.BigViews.Where(a => a.country_id == 1 && a.year_no == current_year && a.month_no == i).Sum(b => b.total);
                double electricNo = (double)dw.BigViews.Where(a => a.country_id == 2 && a.year_no == current_year && a.month_no == i).Sum(b => b.electric);
                //double totalcarsminuselectric = total - electric;



                // cvtemp3.Add(electricNo);
                // elbilNoSeries.Values = cvtemp3;
            }
        }
Beispiel #3
0
        private void button4_Click(object sender, EventArgs e)
        {
            using (CarsDWEntities dw = new CarsDWEntities())
            {
                List <MarketEvent> me = dw.MarketEvents.ToList();

                List <string> meCSVFormattedList = new List <string>();
                string        columnHeaders      = "marketevent_id;date;country_id;title;description;source";
                meCSVFormattedList.Add(columnHeaders);
                foreach (MarketEvent me2 in me)
                {
                    string csv = me2.marketevent_id + ";" + me2.date + ";" + me2.country_id + ";" + me2.title + ";" + me2.description + ";" + me2.source;
                    meCSVFormattedList.Add(csv);
                }


                string   dPath    = @"C:\MarketEventsCSVFile";
                string   filename = "MarketEvents.csv";
                string[] lines    = meCSVFormattedList.ToArray();
                System.IO.Directory.CreateDirectory(dPath);
                System.IO.File.WriteAllLines(dPath + @"\" + filename, lines);
                MessageBox.Show("File created " + dPath + @"\" + filename, "Exported CSV");
                Process.Start(dPath);
            }
        }
        private void PopulateComboBoxes()
        {
            using (CarsDWEntities dw = new CarsDWEntities())
            {
                comboBox1.DataSource    = dw.Countries.ToList();
                comboBox1.DisplayMember = "name";
                comboBox1.ValueMember   = "country_id";


                comboBox2.Items.Add(new CbItem("Electric Cars", "CarSales", "electric", "sum"));
                comboBox2.Items.Add(new CbItem("Hybrid Cars", "CarSales", "hybrids", "sum"));
                comboBox2.Items.Add(new CbItem("Petrol/Diesel Cars", "CarSales", "liquid_fuel", "sum"));
                comboBox2.Items.Add(new CbItem("All Cars", "CarSales", "total", "sum"));
                comboBox2.Items.Add(new CbItem("Co2", "CarSales", "CO2", "sum"));
                comboBox2.Items.Add(new CbItem("Market", "CarSales", "has_market_events", "sum"));



                comboBox2.Items.Add(new CbItem("Average Salary", "Avg_Salary", "avg_salary", "sum"));
                comboBox2.Items.Add(new CbItem("Gas Price", "GasPrice", "gas_price", "sum", true));
                comboBox2.Items.Add(new CbItem("Charging Points", "ChargingPoint", "charging_points", "sum"));
                comboBox2.SelectedIndex = 0;

                comboBox3.Items.Add(new CbItem("Year"));
                comboBox3.Items.Add(new CbItem("Month"));
                comboBox3.SelectedIndex = 0;

                comboBox4.Items.Add(new CbItem("Column"));
                comboBox4.Items.Add(new CbItem("Line"));
                comboBox4.SelectedIndex = 0;
            }
        }
Beispiel #5
0
 private void PopulateGridview()
 {
     using (CarsDWEntities dw = new CarsDWEntities())
     {
         dataGridView1.AutoGenerateColumns = false;
         dataGridView1.DataSource          = dw.MarketEvents.ToList <MarketEvent>();
     }
 }
Beispiel #6
0
        private void button3_Click(object sender, EventArgs e)
        {
            MarketEvent me         = new MarketEvent();
            int         country_id = 0;

            if (Int32.TryParse(textBox1.Text, out country_id))
            {
                //country_id en siffra, gör resten
                me.country_id = country_id;
            }

            //var date = DateTime.Parse(textBox2).text;

            if (DateTime.TryParse(textBox2.Text, out DateTime date))
            {
                me.date = date.Date;
            }
            me.title       = textBox3.Text;
            me.description = richTextBox1.Text;
            me.source      = textBox4.Text;
            if (Int32.TryParse(textBox5.Text, out int marketevent_id))
            {
                //update
                //country_id en siffra, gör resten
                me.marketevent_id = marketevent_id;
                using (CarsDWEntities dw = new CarsDWEntities())
                {
                    var entry = dw.Entry(me);
                    if (entry.State == EntityState.Detached)
                    {
                        dw.MarketEvents.Attach(me);
                    }
                    dw.MarketEvents.Remove(me);
                    int code = dw.SaveChanges();
                    if (code == 1)
                    {
                        label5.Text = "Deleted" + me.title + "code " + code.ToString();
                        PopulateGridview();
                        Clear();
                    }
                    else
                    {
                        label5.Text = "Something went wrong. Code:" + code.ToString();
                    }
                }
            }
            else
            {
                label5.Text = "marketevent_id";
            }
        }
        public void testSourceCode()
        {
            //AUTO GENERATED SOURCE CODE FOR LIVE CHART USING CARSDWENTITIES

            cartesianChart1.AxisX.Clear();
            cartesianChart1.AxisY.Clear();
            using (CarsDWEntities dw = new CarsDWEntities())
            {
                List <BigView> Context = dw.BigViews.Where(c => c.country_id == 2).ToList();


                string        country_name = dw.Countries.Where(c => c.country_id == 2).Select(a => a.name).FirstOrDefault();
                List <double> yAsDouble    = new List <double>();

                List <string> x = new List <string>();
                for (int i = 2011; i <= 2016; i++) //för varje år
                {
                    x.Add(i.ToString());           //year as x values
                    var hjalp = Context.Where(b => b.year_no == i).Select("avg_salary");
                    int sum   = 0;
                    //summerar
                    foreach (int h in hjalp)
                    {
                        sum += h;
                    }
                    //lägger till
                    yAsDouble.Add(sum);
                }
                ChartValues <double> cvy = new ChartValues <double>();
                cvy.AddRange(yAsDouble.ToArray());
                ColumnSeries cs = new ColumnSeries();


                cs.Title     = "Norge" + " avg_salary";
                cs.Values    = cvy;
                cs.ScalesYAt = 0;
                cartesianChart1.AxisX.Add(new Axis
                {
                    Title  = "Year",
                    Labels = x.ToArray()
                });

                cartesianChart1.AxisY.Add(new Axis
                {
                    Title          = "avg_salary",
                    LabelFormatter = value => value.ToString()
                });
                cartesianChart1.Series.Add(cs);
            }
        }
        private void DrawPieChart1()
        {
            Func <ChartPoint, string> labelPoint = chartPoint =>
                                                   string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);
            double totalcarsminuselectric = 0;
            double electric = 0;


            otherCarsSveSeries = new PieSeries
            {
                Title      = "Övriga bilar",
                DataLabels = false,
                LabelPoint = labelPoint
            };


            elbilSveSeries = new PieSeries
            {
                Title      = "Elbilar",
                DataLabels = false,
                PushOut    = 2,
                LabelPoint = labelPoint
            };



            pieChart1.LegendLocation        = LegendLocation.Bottom;
            pieChart1.StartingRotationAngle = -40;

            using (CarsDWEntities dw = new CarsDWEntities())
            {
                int i = year_start;

                double total = (double)dw.BigViews.Where(a => a.country_id == 1 && a.year_no == i).Sum(b => b.total);
                electric = (double)dw.BigViews.Where(a => a.country_id == 1 && a.year_no == i).Sum(b => b.electric);
                totalcarsminuselectric = total - electric;

                ChartValues <ObservableValue> cvOther    = new ChartValues <ObservableValue>();
                ChartValues <ObservableValue> cvElectric = new ChartValues <ObservableValue>();
                //SVERIGE
                //Sätter den statisk ist för visualiseringspurposes
                cvOther.Add(new ObservableValue(1000)); //snabbt uppskattad avg per mån
                cvElectric.Add(new ObservableValue(electric));
                otherCarsSveSeries.Values = cvOther;
                elbilSveSeries.Values     = cvElectric;

                pieChart1.Series.Add(otherCarsSveSeries);
                pieChart1.Series.Add(elbilSveSeries);
            }
        }
        private void cartesianChart1_DataHover(object sender, ChartPoint p)
        {
            if (xStartsAtDateMonth == 0 && xStartsAtDateYear != 0)
            {
                //år verkar valt
                int yr = (int)p.X + xStartsAtDateYear;



                using (CarsDWEntities dw = new CarsDWEntities())
                {
                    if (p.SeriesView.Title.Contains("Norge")) //Norway
                    {
                        var me = dw.MarketEvents.Where(a => a.year_no == yr && a.country_id == 2).Select(a => new { a.title, a.date, a.country_name, a.description }).ToList();

                        dataGridView1.DataSource = me;
                    }
                    else if (p.SeriesView.Title.Contains("Sverige"))
                    {
                        var me = dw.MarketEvents.Where(a => a.year_no == yr && a.country_id == 1).Select(a => new { a.title, a.date, a.country_name, a.description }).ToList();

                        dataGridView1.DataSource = me;
                    }
                }
                // label1.Text = yr.ToString();
            }
            else if (xStartsAtDateYear != 0 && xStartsAtDateMonth != 0)
            {
                //räknar antalet månader fram
                DateTime d = new DateTime(xStartsAtDateYear, 1, 1);
                d = d.AddMonths((int)p.X);
                int yr   = d.Year;
                int mnth = d.Month;
                using (CarsDWEntities dw = new CarsDWEntities())
                {
                    if (p.SeriesView.Title.Contains("Norge")) //norway
                    {
                        var me = dw.MarketEvents.Where(a => a.year_no == yr && a.month_no == mnth && a.country_id == 2).Select(a => new { a.date, a.country_name, a.description, a.title }).ToList();
                        dataGridView1.DataSource = me;
                    }
                    else if (p.SeriesView.Title.Contains("Sverige")) //sweden
                    {
                        var me = dw.MarketEvents.Where(a => a.year_no == yr && a.month_no == mnth && a.country_id == 1).Select(a => new { a.date, a.country_name, a.description, a.title }).ToList();
                        dataGridView1.DataSource = me;
                    }
                }
                // label1.Text = yr.ToString() + mnth.ToString();
            }
        }
        public void setGridView(int index)
        {
            int year = int.TryParse(years[index], out int value) ? value : 2016;

            using (CarsDWEntities dw = new CarsDWEntities())
            {
                bindingNor.DataSource = dw.MarketEvents.Where(a => a.year_no == year && a.country_id == 2).Select(a => new { a.title, a.date, a.description, a.country_name }).ToList();

                dgvNor.DataSource = bindingNor;

                var sme = dw.MarketEvents.Where(a => a.year_no == year && a.country_id == 1).Select(a => new { a.title, a.date, a.description, a.country_name }).ToList();
                dgvSwe.DataSource = null;
                dgvSwe.Rows.Clear();
                dgvSwe.Refresh();

                dgvSwe.DataSource = sme;
            }
        }
        private void DrawPieChart2()
        {
            Func <ChartPoint, string> labelPoint = chartPoint =>
                                                   string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);
            double totalcarsminuselectric = 0;
            double electric = 0;

            using (CarsDWEntities dw = new CarsDWEntities())
            {
                double total = (double)dw.BigViews.Where(a => a.country_id == 2 && a.year_no == 2016).Sum(b => b.total);
                electric = (double)dw.BigViews.Where(a => a.country_id == 2 && a.year_no == 2016).Sum(b => b.electric);
                totalcarsminuselectric = total - electric;
            }

            otherCarsNoSeries = new PieSeries
            {
                Title      = "Övriga bilar Norge",
                DataLabels = false,
                LabelPoint = labelPoint
            };


            elbilNoSeries = new PieSeries
            {
                Title      = "Elbilar Norge",
                DataLabels = false,
                PushOut    = 2,
                LabelPoint = labelPoint
            };

            ChartValues <double> cvOther    = new ChartValues <double>();
            ChartValues <double> cvElectric = new ChartValues <double>();

            cvOther.Add(12000);
            cvElectric.Add(electric);
            otherCarsNoSeries.Values = cvOther;
            elbilNoSeries.Values     = cvElectric;
            pieChart2.Series.Add(otherCarsNoSeries);
            pieChart2.Series.Add(elbilNoSeries);


            pieChart2.LegendLocation = LegendLocation.Bottom;
        }
Beispiel #12
0
        private void marcusPlot()

        {
            List <int> ylista = new List <int>();

            using (CarsDWEntities mw = new CarsDWEntities()) //using för att den är väldigt tung. Vill stänga när vi använt databasen
            {
                ylista = mw.BigViews.Select(q => (int)q.electric).ToList();
            }
            ChartValues <int> cw = new ChartValues <int>();

            cw.AddRange(ylista);
            LineSeries ls = new LineSeries();

            ls.Title  = "electric";
            ls.Values = cw;

            cartesianChart1.Series.Add(ls);
        }
        private void plot()
        {
            DateTime       startDate = new DateTime(2011, 1, 1);
            DateTime       endDate   = new DateTime(2017, 1, 1);
            List <BigView> Context   = new List <BigView>();

            System.Collections.Generic.List <double> swePro = new System.Collections.Generic.List <double>();
            System.Collections.Generic.List <double> norPro = new System.Collections.Generic.List <double>();

            using (CarsDWEntities dw = new CarsDWEntities())
            {
                Context = dw.BigViews.Where(filter => filter.date >= startDate && filter.date < endDate).ToList();
            }
            {
                var co2 = Context.Select(f => new { year = f.year_no, co = f.CO2, coun = f.country_id });
                System.Collections.Generic.List <double> procSalesSweden = Context.Where(c => c.country_id == 1).Select(f => new { year = f.year_no, total = f.total, electric = f.electric }).GroupBy(g => g.year, g => ((double)g.electric / (double)g.total * 100), (Key, g) => new { year = Key, proc = g.Average() }).Select(y => y.proc).ToList();

                System.Collections.Generic.List <double> procSalesNor = Context.Where(c => c.country_id == 2).Select(f => new { year = f.year_no, total = f.total, electric = f.electric }).GroupBy(g => g.year, g => ((double)g.electric / (double)g.total * 100), (Key, g) => new { year = Key, proc = g.Average() }).Select(y => y.proc).ToList();

                var salesSwe = Context.Where(f => f.country_id == 1).GroupBy(x => x.year_no, (year, y) => new
                {
                    Key   = year,
                    Total = y.Sum(x => x.total),
                    Elec  = y.Sum(x => (int)x.electric)
                });

                var salesNor = Context.Where(f => f.country_id == 2).GroupBy(x => x.year_no, (year, y) => new
                {
                    Key   = year,
                    Total = y.Sum(x => x.total),
                    Elec  = y.Sum(x => (int)x.electric)
                });



                ChartValues <int> electricSweden = new ChartValues <int>();
                electricSweden.AddRange(salesSwe.Select(x => x.Elec).ToList());
                ChartValues <int> totalSweden = new ChartValues <int>();
                totalSweden.AddRange(salesSwe.Select(x => x.Total - x.Elec).ToList());

                ChartValues <int> electricNorway = new ChartValues <int>();
                electricNorway.AddRange(salesNor.Select(x => x.Elec).ToList());
                ChartValues <int> totalNorway = new ChartValues <int>();
                totalNorway.AddRange(salesNor.Select(x => x.Total - x.Elec).ToList());


                cartesianChart1.AxisX.Add(new Axis
                {
                    Title  = "Year",
                    Labels = new[] { "2011", "2012", "2013", "2014", "2015", "2016" }
                });

                cartesianChart1.Series = new SeriesCollection
                {
                    new StackedAreaSeries
                    {
                        Title          = "Total Sweden",
                        Values         = totalSweden,
                        LineSmoothness = 0
                    },
                    new StackedAreaSeries
                    {
                        Values         = electricSweden,
                        Title          = "Electric Sales Sweden",
                        LineSmoothness = 0
                    },
                    new StackedAreaSeries
                    {
                        Values         = electricNorway,
                        Title          = "Electric Sales Norway",
                        LineSmoothness = 0
                    },
                    new StackedAreaSeries
                    {
                        Title          = "Total Sales Norway",
                        Values         = totalNorway,
                        LineSmoothness = 0
                    }
                };


                cartesianChart1.LegendLocation = LegendLocation.Right;


                System.Collections.Generic.List <double> co2sweden = co2.Where(i => i.coun == 1).GroupBy(g => g.year, g => g.co, (key, g) => new { year = key, co2 = (double)g.Sum() / 12 }).Select(filter => filter.co2).ToList();
                System.Collections.Generic.List <double> co2norway = co2.Where(i => i.coun == 2).GroupBy(g => g.year, g => g.co, (key, g) => new { year = key, co2 = (double)g.Sum() / 12 }).Select(filter => filter.co2).ToList();

                ChartValues <double> NCV = new ChartValues <double>();
                NCV.AddRange(co2sweden);
                ChartValues <double> SCV = new ChartValues <double>();
                SCV.AddRange(co2norway);

                cartesianChart1.AxisY.Add(new Axis {
                    Title = "CarSales", MinValue = 0
                });
                cartesianChart1.AxisY.Add(new Axis {
                    Title = "Co2", MinValue = 0, Position = AxisPosition.RightTop
                });
                LineSeries NLS = new LineSeries
                {
                    Title           = "Norge",
                    Values          = NCV,
                    PointGeometry   = null,
                    StrokeThickness = 4,
                    ScalesYAt       = 1
                };

                LineSeries SLS = new LineSeries
                {
                    Title           = "Svedala",
                    Values          = SCV,
                    PointGeometry   = null,
                    StrokeThickness = 4,
                    ScalesYAt       = 1
                };

                cartesianChart1.Series.Add(SLS);
                cartesianChart1.Series.Add(NLS);
                cartesianChart1.LegendLocation = LegendLocation.Top;
                System.Windows.Controls.Panel.SetZIndex(SLS, 10);
                System.Windows.Controls.Panel.SetZIndex(NLS, 10);
            }
        }
        private void plot2()
        {
            DateTime       startDate = new DateTime(2010, 1, 1);
            DateTime       endDate   = new DateTime(2017, 1, 1);
            List <BigView> Context   = new List <BigView>();

            using (CarsDWEntities dw = new CarsDWEntities())
            {
                Context = dw.BigViews.Where(filter => filter.date >= startDate && filter.date < endDate).ToList();
            }

            var salesSwe = Context.Where(f => f.country_id == 1).GroupBy(x => x.year_no, (year, y) => new
            {
                Key   = year,
                Total = y.Sum(x => x.total),
                Elec  = y.Sum(x => (int)x.electric)
            });

            var salesNor = Context.Where(f => f.country_id == 2).GroupBy(x => x.year_no, (year, y) => new
            {
                Key   = year,
                Total = y.Sum(x => x.total),
                Elec  = y.Sum(x => (int)x.electric)
            });


            ChartValues <int> electricSweden = new ChartValues <int>();

            electricSweden.AddRange(salesSwe.Select(x => x.Elec).ToList());
            ChartValues <int> totalSweden = new ChartValues <int>();

            totalSweden.AddRange(salesSwe.Select(x => x.Total).ToList());

            ChartValues <int> electricNorway = new ChartValues <int>();

            electricNorway.AddRange(salesNor.Select(x => x.Elec).ToList());
            ChartValues <int> totalNorway = new ChartValues <int>();

            totalNorway.AddRange(salesNor.Select(x => x.Total).ToList());

            //räknar ut procent
            procentSwe = salesSwe.Select(i => ((double)i.Elec / (double)i.Total * 100)).ToList();
            procentNor = salesNor.Select(i => ((double)i.Elec / (double)i.Total * 100)).ToList();

            years = salesNor.Select(x => "" + x.Key).ToList();

            setTxtButtons(years.IndexOf(years.Last()));

            cartesianChart1.AxisX.Add(new Axis
            {
                Title    = "Year",
                FontSize = 20,
                Labels   = years.ToArray()
            });

            electricSalesS = new ColumnSeries
            {
                Values = electricSweden,
                Title  = "Electric Sales Sweden",
                Fill   = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(179, 0, 0))
            };
            totSalesS = new ColumnSeries
            {
                Values = totalSweden,
                Title  = "Total Sales Sweden",
                Fill   = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(255, 102, 102))
            };

            elecSalesN = new ColumnSeries
            {
                Values = electricNorway,
                Title  = "Electric Sales Norway",
                Fill   = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(0, 107, 179))
            };
            totSalesN = new ColumnSeries
            {
                Values = totalNorway,
                Title  = "Total Sales Norway",
                Fill   = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(153, 214, 255))
            };
            cartesianChart1.Series = new SeriesCollection
            {
                totSalesS, electricSalesS, elecSalesN, totSalesN
            };


            var co2 = Context.Select(f => new { year = f.year_no, co = f.CO2, coun = f.country_id });

            List <double> co2sweden = co2.Where(i => i.coun == 1).GroupBy(g => g.year, g => g.co, (key, g) => new { year = key, co2 = (double)g.Sum() / 12 }).Select(filter => filter.co2).ToList();

            List <double> co2norway = co2.Where(i => i.coun == 2).GroupBy(g => g.year, g => g.co, (key, g) => new { year = key, co2 = (double)g.Sum() / 12 }).Select(filter => filter.co2).ToList();

            ChartValues <double> NCV = new ChartValues <double>();

            NCV.AddRange(co2norway);
            ChartValues <double> SCV = new ChartValues <double>();

            SCV.AddRange(co2sweden);

            cartesianChart1.AxisY.Add(new Axis {
                Title = "CarSales", MinValue = 0, LabelFormatter = val => (val / 1000 + "k"), FontSize = 16
            });
            cartesianChart1.AxisY.Add(new Axis {
                Title = "Co2", MinValue = 0, Position = AxisPosition.RightTop, FontSize = 0.1
            });
            LineSeries NLS = new LineSeries
            {
                Title           = "Co2 Norway",
                Values          = NCV,
                PointGeometry   = null,
                Stroke          = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(0, 107, 179)),
                Fill            = System.Windows.Media.Brushes.Transparent,
                StrokeThickness = 4,
                ScalesYAt       = 1,
            };

            LineSeries SLS = new LineSeries
            {
                Title           = "Co2 Sweden",
                Values          = SCV,
                PointGeometry   = null,
                Stroke          = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(179, 0, 0)),
                Fill            = System.Windows.Media.Brushes.Transparent,
                StrokeThickness = 4,
                ScalesYAt       = 1
            };

            cartesianChart1.Series.Add(SLS);
            cartesianChart1.Series.Add(NLS);
            cartesianChart1.LegendLocation = LegendLocation.Top;
            System.Windows.Controls.Panel.SetZIndex(SLS, 10);
            System.Windows.Controls.Panel.SetZIndex(NLS, 10);


            cartesianChart1.AxisX[0].MinValue = 1;
            cartesianChart1.AxisX[0].MaxValue = 7;
        }
Beispiel #15
0
        private void button1_Click(object sender, EventArgs e)
        {
            MarketEvent me         = new MarketEvent();
            int         country_id = 0;

            if (Int32.TryParse(textBox1.Text, out country_id))
            {
                //country_id en siffra, gör resten
                me.country_id = country_id;
            }

            //var date = DateTime.Parse(textBox2).text;

            if (DateTime.TryParse(textBox2.Text, out DateTime date))
            {
                me.date = date.Date;
            }
            me.title       = textBox3.Text;
            me.description = richTextBox1.Text;
            me.source      = textBox4.Text;
            string wtf = textBox5.Text;

            label5.Text = country_id.ToString() + me.date.ToString() + textBox5.Text + button1.Text;
            if (country_id != 0 && me.date != null && textBox5.Text == "" && button1.Text == "Insert")
            {
                using (CarsDWEntities dw = new CarsDWEntities())
                {
                    dw.MarketEvents.Add(me);
                    int code = dw.SaveChanges();
                    if (code == 1)
                    {
                        label5.Text = "Inserted" + me.title + "code " + code.ToString();
                        PopulateGridview();
                        Clear();
                    }
                    else
                    {
                        label5.Text = "Something went wrong. code " + code.ToString();
                    }
                }
            }
            else if (button1.Text == "Update" && textBox5.Text != "")
            {
                if (Int32.TryParse(textBox5.Text, out int marketevent_id))
                {
                    //update
                    //country_id en siffra, gör resten
                    me.marketevent_id = marketevent_id;
                    using (CarsDWEntities dw = new CarsDWEntities())
                    {
                        dw.Entry(me).State = EntityState.Modified;

                        int code = dw.SaveChanges();
                        if (code == 1)
                        {
                            label5.Text = "Updated" + me.title + "code " + code.ToString();
                            PopulateGridview();
                            Clear();
                        }
                        else
                        {
                            label5.Text = "Something went wrong. Code:" + code.ToString();
                        }
                    }
                }
            }
        }
        private void DrawSveNoSkillnadAntal()
        {
            //AUTO GENERATED SOURCE CODE FOR LIVE CHART USING CARSDWENTITIES

            cartesianChart1.AxisX.Clear();
            cartesianChart1.AxisY.Clear();
            using (CarsDWEntities dw = new CarsDWEntities())
            {
                List <BigView> Context = dw.BigViews.Where(c => c.country_id == 2).ToList();


                string        country_name = dw.Countries.Where(c => c.country_id == 2).Select(a => a.name).FirstOrDefault();
                List <double> yAsDouble    = new List <double>();

                List <string> x = new List <string>();
                for (int i = 2011; i <= 2016; i++) //för varje år
                {
                    //addarer y 12 gånger per år el hur många månader som det nu finns
                    int maxmonth = Context.Where(C => C.year_no == i).Max(m => m.month_no);
                    int minmonth = Context.Where(C => C.year_no == i).Min(m => m.month_no);

                    for (int m = minmonth; m <= maxmonth; m++) //för månad
                    {
                        var hjalp1 = Context.Where(b => b.year_no == i && b.month_no == m).Select("electric");
                        int sumM   = 0;
                        //summerar
                        foreach (int h in hjalp1)
                        {
                            sumM += h;
                        }
                        //lägger till
                        yAsDouble.Add(sumM);


                        x.Add(i.ToString() + "-" + m.ToString());
                    }
                }
                ChartValues <double> cvy = new ChartValues <double>();
                cvy.AddRange(yAsDouble.ToArray());
                ColumnSeries cs = new ColumnSeries();


                cs.Title     = "Norge" + " electric";
                cs.Values    = cvy;
                cs.ScalesYAt = 0;
                cartesianChart1.AxisX.Add(new Axis
                {
                    Title  = "Month",
                    Labels = x.ToArray()
                });

                cartesianChart1.AxisY.Add(new Axis
                {
                    Title          = "electric",
                    LabelFormatter = value => value.ToString()
                });
                cartesianChart1.Series.Add(cs);
            }
            using (CarsDWEntities dw = new CarsDWEntities())
            {
                List <BigView> Context = dw.BigViews.Where(c => c.country_id == 1).ToList();


                string        country_name = dw.Countries.Where(c => c.country_id == 1).Select(a => a.name).FirstOrDefault();
                List <double> yAsDouble    = new List <double>();

                List <string> x = new List <string>();
                for (int i = 2011; i <= 2016; i++) //för varje år
                {
                    //addarer y 12 gånger per år el hur många månader som det nu finns
                    int maxmonth = Context.Where(C => C.year_no == i).Max(m => m.month_no);
                    int minmonth = Context.Where(C => C.year_no == i).Min(m => m.month_no);

                    for (int m = minmonth; m <= maxmonth; m++) //för månad
                    {
                        var hjalp1 = Context.Where(b => b.year_no == i && b.month_no == m).Select("electric");
                        int sumM   = 0;
                        //summerar
                        foreach (int h in hjalp1)
                        {
                            sumM += h;
                        }
                        //lägger till
                        yAsDouble.Add(sumM);


                        x.Add(i.ToString() + "-" + m.ToString());
                    }
                }
                ChartValues <double> cvy = new ChartValues <double>();
                cvy.AddRange(yAsDouble.ToArray());

                ColumnSeries cs = new ColumnSeries();
                cs.Title     = "Sverige" + " electric";
                cs.Values    = cvy;
                cs.ScalesYAt = 0; cartesianChart1.Series.Add(cs);
            }
        }
        public void Plot()
        {
            //AUTO GENERATED SOURCE CODE FOR LIVE CHART USING CARSDWENTITIES

            cartesianChart1.AxisX.Clear();
            cartesianChart1.AxisY.Clear();
            using (CarsDWEntities dw = new CarsDWEntities())
            {
                List <BigView> Context = dw.BigViews.Where(c => c.country_id == 2).ToList();


                string        country_name = dw.Countries.Where(c => c.country_id == 2).Select(a => a.name).FirstOrDefault();
                List <double> yAsDouble    = new List <double>();

                List <string> x = new List <string>();
                for (int i = 2014; i <= 2017; i++) //för varje år
                {
                    x.Add(i.ToString());           //year as x values
                    var    hjalp = Context.Where(b => b.year_no == i).Select("gas_price");
                    double sum   = 0;
                    //summerar
                    foreach (double h in hjalp)
                    {
                        sum += h;
                    }
                    //lägger till
                    yAsDouble.Add(sum);
                }
                ChartValues <double> cvy = new ChartValues <double>();
                cvy.AddRange(yAsDouble.ToArray());
                LineSeries ls = new LineSeries();


                ls.Title     = "Norge";
                ls.Values    = cvy;
                ls.ScalesYAt = 0;
                cartesianChart1.AxisX.Add(new Axis
                {
                    Title  = "Year",
                    Labels = x.ToArray()
                });

                cartesianChart1.AxisY.Add(new Axis
                {
                    Title          = "Pris/l SEK",
                    LabelFormatter = value => value.ToString()
                });
                cartesianChart1.Series.Add(ls);
            }
            using (CarsDWEntities dw = new CarsDWEntities())
            {
                List <BigView> Context = dw.BigViews.Where(c => c.country_id == 1).ToList();


                string        country_name = dw.Countries.Where(c => c.country_id == 1).Select(a => a.name).FirstOrDefault();
                List <double> yAsDouble    = new List <double>();

                List <string> x = new List <string>();
                for (int i = 2014; i <= 2017; i++) //för varje år
                {
                    x.Add(i.ToString());           //year as x values
                    var    hjalp = Context.Where(b => b.year_no == i).Select("gas_price");
                    double sum   = 0;
                    //summerar
                    foreach (double h in hjalp)
                    {
                        sum += h;
                    }
                    //lägger till
                    yAsDouble.Add(sum);
                }
                ChartValues <double> cvy = new ChartValues <double>();
                cvy.AddRange(yAsDouble.ToArray());

                LineSeries ls = new LineSeries();
                ls.Title     = "Sverige";
                ls.Values    = cvy;
                ls.ScalesYAt = 0; cartesianChart1.Series.Add(ls);
            }
        }
Beispiel #18
0
        // Test function for the cartesianchart.
        private void cartesianPlot()
        {
            List <BigView> Context = new List <BigView>();

            System.Collections.Generic.List <double> swePro = new System.Collections.Generic.List <double>();
            System.Collections.Generic.List <double> norPro = new System.Collections.Generic.List <double>();
            using (CarsDWEntities dw = new CarsDWEntities())
            {
                Context = dw.BigViews.Where(filter => filter.date >= new DateTime(2011, 1, 1) && filter.date < new DateTime(2017, 1, 1)).ToList();
                var dataSweden = dw.BigViews.Where(e => e.date > new DateTime(2011, 1, 1) && e.country_id == 1).Select(Q => new { ev = (double)Q.electric, tot = (double)Q.total }).Select(QW => new { result = (QW.ev / QW.tot) * 100 });
                var dataNoway  = dw.BigViews.Where(e => e.date > new DateTime(2011, 1, 1) && e.country_id == 2).Select(Q => new { ev = (double)Q.electric, tot = (double)Q.total }).Select(QW => new { result = (QW.ev / QW.tot) * 100 });

                //swePro = Context.Where(c => c.country_id == 1).Select(s => new { tot = (double)s.electric / s.total }).Select(t => t.tot).ToList();//dataSweden.Select(x => x.result).ToList();
                swePro = dataSweden.Select(x => x.result).ToList();
                norPro = dataNoway.Select(x => x.result).ToList();
            }

            /*var No = dw.BigViews.Where(e => e.date > new DateTime(2011, 1, 1) && e.carsales_id == 2).Select(x => new { total = (double)x.total, elec = (double)x.electric,date = x.date });
             *  System.Collections.Generic.List<int> norge = new System.Collections.Generic.List<int>();
             *  norge = dw.BigViews.Where(e => e.date > new DateTime(2011, 1, 1) && e.country_id == 2).Select(Q => (int)Q.electric).ToList();
             *  System.Collections.Generic.List<int> svergie = new System.Collections.Generic.List<int>();
             *  svergie =
             *  dw.BigViews.Where(e => e.date > new DateTime(2011, 1, 1) && e.carsales_id == 1).Select(x => new { proc = (double)x.electric / (double)x.total });
             *  var proc = No.Select(y => new { proc = (y.total / y.elec) });
             */
            ChartValues <double> NCV = new ChartValues <double>();

            NCV.AddRange(norPro);
            ChartValues <double> SCV = new ChartValues <double>();

            SCV.AddRange(swePro);

            LineSeries NLS = new LineSeries();

            NLS.Title           = "Norge";
            NLS.Values          = NCV;
            NLS.PointGeometry   = null;
            NLS.StrokeThickness = 4;

            LineSeries SLS = new LineSeries();

            SLS.Title           = "Svedala";
            SLS.Values          = SCV;
            SLS.PointGeometry   = null;
            SLS.StrokeThickness = 4;

            cartesianChart1.Series = new SeriesCollection
            {
                NLS, SLS
            };

            cartesianChart1.AxisX.Add(new Axis
            {
                MinValue = 0,
                MaxValue = 11
            });

            cartesianChart1.AxisY.Add(new Axis
            {
                Title = "Procent",

                MinValue       = 0,
                MaxValue       = 50,
                LabelFormatter = val => val + "%",
                Separator      = new Separator
                {
                    Step      = 5,
                    IsEnabled = false
                }
            });
            cartesianChart1.LegendLocation = LegendLocation.Right;

            cartesianChart1.DataClick += CartesianChart1OnDataClick;

            /*
             * cartesianChart1.AxisX.Add(new Axis
             * {
             *  Title = "Month",
             *  Labels = new[] { "Jan", "Feb", "Mar", "Apr", "May" }
             * });
             *
             * cartesianChart1.AxisY.Add(new Axis
             * {
             *  Title = "Procent"
             *  //LabelFormatter = value => value.ToString("C")
             * });
             *
             *
             * //modifying the series collection will animate and update the chart
             *
             * cartesianChart1.Series.Add(new LineSeries
             * {
             *  Values = new ChartValues<double> { 5, 3, 2, 4, 5 },
             *  LineSmoothness = 0, //straight lines, 1 really smooth lines
             *  PointGeometry = Geometry.Parse("m 25 70.36218 20 -28 -20 22 -8 -6 z"),
             *  PointGeometrySize = 50,
             *  PointForeground = Brushes.Gray
             * });
             *
             * //modifying any series values will also animate and update the chart
             * //cartesianChart1.Series[2].Values.Add(5d);
             */
        }
        private void DrawGraph(int country_id, string xtitle, string ytitle, string pickedColumn, string typeofcalculation, string graphtype, string granularity, int start_year_no, int end_year_no, bool add, bool isFloat, bool addScaleY)
        {
            if (add == false)
            {
                xStartsAtDateYear  = 0;
                xStartsAtDateMonth = 0;
                currentYscaleLs    = 0;
                currentYscaleCs    = 0;
                cartesianChart1.AxisX.Clear();
                cartesianChart1.AxisY.Clear();
            }

            using (CarsDWEntities dw = new CarsDWEntities())
            {
                List <BigView> Context = dw.BigViews.Where(c => c.country_id == country_id).ToList();


                string country_name = dw.Countries.Where(c => c.country_id == country_id).Select(a => a.name).FirstOrDefault();
                //per år
                //hämtar min o max
                int max = Context.Max(a => a.year_no);
                int min = Context.Min(a => a.year_no);
                if (!(start_year_no < min))
                {
                    min = start_year_no;
                }
                if (!(end_year_no > max))
                {
                    max = end_year_no;
                }
                //MOUSE HOVER TA EJ BORT
                if (xStartsAtDateYear == 0)
                {
                    xStartsAtDateYear = min;
                }

                GenerateSourceCodeGraph(country_id, xtitle, ytitle, pickedColumn, typeofcalculation, graphtype, granularity, min, max, add, isFloat, addScaleY);



                List <int>    y         = new List <int>();
                List <double> yAsDouble = new List <double>();

                List <string> x = new List <string>();
                for (int i = min; i <= max; i++) //för varje år
                {
                    switch (granularity.ToLower())
                    {
                    case "year":


                        x.Add(i.ToString());    //year as x values INTRESSANT RAD
                        var hjalp = Context.Where(b => b.year_no == i).Select(pickedColumn);
                        if (!isFloat)
                        {
                            int sum = 0;
                            //summerar
                            foreach (int h in hjalp)
                            {
                                sum += h;
                            }
                            //lägger till
                            y.Add(sum);
                        }
                        else     //float
                        {
                            double sum = 0;
                            //summerar
                            foreach (double h in hjalp)
                            {
                                sum += h;
                            }
                            //lägger till
                            yAsDouble.Add(sum);
                        }

                        break;

                    case "month":
                        //addarer y 12 gånger per år el hur många månader som det nu finns
                        int maxmonth = Context.Where(C => C.year_no == i).Max(m => m.month_no);
                        int minmonth = Context.Where(C => C.year_no == i).Min(m => m.month_no);
                        //hjälp till hoverfunktionen TA EJ BORT
                        if (xStartsAtDateMonth == 0)
                        {
                            xStartsAtDateMonth = minmonth;
                        }
                        for (int m = minmonth; m <= maxmonth; m++)     //för månad
                        {
                            var hjalp1 = Context.Where(b => b.year_no == i && b.month_no == m).Select(pickedColumn);
                            if (!isFloat)
                            {
                                int sumM = 0;
                                //summerar
                                foreach (int h in hjalp1)
                                {
                                    sumM += h;
                                }
                                //lägger till
                                y.Add(sumM);
                            }
                            else     //floathjälp1
                            {
                                double sumM = 0;
                                //summerar
                                foreach (double h in hjalp1)
                                {
                                    sumM += h;
                                }
                                //lägger till
                                yAsDouble.Add(sumM);
                            }



                            x.Add(i.ToString() + "-" + m.ToString());
                        }

                        break;

                    default:
                        break;
                    }
                }
                //Klar med listorna av x o y värden


                ChartValues <double> cvy = new ChartValues <double>();

                if (!isFloat) //integer
                {
                    cvy.AddRange(y.Select(a => (double)a).ToArray());
                }
                else //double
                {
                    cvy.AddRange(yAsDouble.ToArray());
                }
                //typ av graf y värden
                ColumnSeries cs = new ColumnSeries();
                LineSeries   ls = new LineSeries();

                //Hanterar skalor och lägger in värden i series
                switch (graphtype.ToLower())
                {
                case "column":
                    cs.Title  = country_name + " " + pickedColumn;
                    cs.Values = cvy;
                    //man vill lägga till en skala, ökar skalindex på resp
                    if (addScaleY == true)
                    {
                        currentYscaleCs++;
                    }
                    cs.ScalesYAt = currentYscaleCs;
                    break;

                case "line":
                    ls.Title  = country_name + " " + pickedColumn;
                    ls.Values = cvy;
                    //ökar skalindex
                    if (addScaleY == true)
                    {
                        currentYscaleLs++;
                    }
                    ls.ScalesYAt = currentYscaleLs;
                    break;

                default:
                    break;
                }


                int numberofXAxis = cartesianChart1.AxisX.Count();
                int numberofYAxis = cartesianChart1.AxisY.Count();
                //stoppar bara in x om det inte finns
                if (numberofXAxis == 0)
                {
                    cartesianChart1.AxisX.Add(new Axis
                    {
                        Title  = xtitle,
                        Labels = x.ToArray()
                    });

                    cartesianChart1.AxisY.Add(new Axis
                    {
                        Title          = ytitle,
                        LabelFormatter = value => value.ToString()
                    });
                }
                else if (addScaleY == true && numberofXAxis != 0 && add == true) //det är inte den första graphen och man vill lägga till en ny skala
                {
                    cartesianChart1.AxisY.Add(new Axis
                    {
                        Title          = ytitle,
                        LabelFormatter = value => value.ToString()
                    });
                }

                if (add)
                {
                    switch (graphtype.ToLower())
                    {
                    case "column":
                        cartesianChart1.Series.Add(cs);
                        break;

                    case "line":
                        cartesianChart1.Series.Add(ls);
                        break;

                    default:
                        break;
                    }
                }
                else
                {
                    switch (graphtype.ToLower())
                    {
                    case "column":
                        cartesianChart1.Series.Clear();
                        cartesianChart1.Series.Add(cs);
                        break;

                    case "line":
                        cartesianChart1.Series.Clear();
                        cartesianChart1.Series.Add(ls);
                        break;

                    default:
                        break;
                    }
                }
            }
        }