Моя программа состоит из диаграммы из библиотеки LiveCharts, которая привязана к запросу sqlite, этот запрос sqlite будет меняться в зависимости от выбранного элемента поля со списком, я хочу, чтобы это изменение в запросе sqlite также означало изменение диаграмма. вот мой код:
cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using LiveCharts;
using LiveCharts.Wpf;
using System.Data;
using System.Data.SQLite;
using INotify;
namespace example
{
///
/// Lógica de interacción para Charts.xaml
///
public partial class Charts : Window
{
string dow = "";
public Charts()
{
InitializeComponent();
comboBox.SelectedIndex = 1;
}
public SeriesCollection SeriesCollection { get; set; }
public string[] Labels { get; set; }
public Func Formatter { get; set; }
private void ComboBox_DropDownClosed_1(object sender, EventArgs e)
{
ComboBoxItem cbi = (ComboBoxItem)comboBox.SelectedItem;
dow = cbi.Content.ToString();
label.Content = dow;
List amount = new List();
List product = new List();
DatabaseHelper dbh = new DatabaseHelper();
SQLiteConnection connection;
connection = dbh.Createconnection();
String loadchart = "SELECT product, count(product) AS Amount FROM table_productsold WHERE DATE like '" + dow + "%' GROUP BY product; ";
SQLiteCommand command = new SQLiteCommand(loadchart, connection);
SQLiteDataReader reader;
reader = command.ExecuteReader();
while (reader.Read())
{
amount.Add(Convert.ToInt32(reader["Amount"]));
product.Add(reader["product"].ToString());
}
SeriesCollection = new SeriesCollection
{
new ColumnSeries
{
Title = "2015",
Values = new ChartValues(amount)
}
};
//adding series will update and animate the chart automatically
Labels = product.ToArray();
Formatter = value => value.ToString("N");
DataContext = this;
connection.Close();
dow = "";
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using System.Windows;
namespace example
{
class DatabaseHelper
{
public SQLiteConnection Createconnection()
{
SQLiteConnection connection;
connection = new SQLiteConnection(@"Data Source=C:\Users\Alan\source\repos\example\example\example.db;New=True;Compress=True;" );
try
{
connection.Open();
MessageBox.Show("Could connect ");
}
catch (Exception ex)
{
MessageBox.Show("Could not connect " + ex.Message);
}
return connection;
}
public void createtablelogs(SQLiteConnection conn)
{
SQLiteCommand command;
string ctl = @"CREATE TABLE IF NOT EXISTS table_productsold(product TEXT NOT NULL, price int, DATE TEXT NOT NULL);";
command = conn.CreateCommand();
command.CommandText = ctl;
command.ExecuteNonQuery();
}
public void insertProductsold(SQLiteConnection conn, string products, int prices)
{
SQLiteCommand command;
string insert = "INSERT INTO table_productsold VALUES('"+products+"',"+prices+", case cast(strftime('%w', 'now', 'localtime')as integer)" +
"when 0 then 'Dom' ||' '|| strftime('%Y/%m/%d, %H:%M', 'now', 'localtime') " +
"when 1 then 'Lun' ||' '|| strftime('%Y/%m/%d, %H:%M', 'now', 'localtime')" +
"when 2 then 'Mar' ||' '|| strftime('%Y/%m/%d, %H:%M', 'now', 'localtime')" +
"when 3 then 'Mie' ||' '|| strftime('%Y/%m/%d, %H:%M', 'now', 'localtime')" +
"when 4 then 'Jue' ||' '|| strftime('%Y/%m/%d, %H:%M', 'now', 'localtime')" +
"when 5 then 'Vie' ||' '|| strftime('%Y/%m/%d, %H:%M', 'now', 'localtime')" +
"when 6 then 'Sab' ||' '|| strftime('%Y/%m/%d, %H:%M', 'now', 'localtime')" +
"end);";
command = conn.CreateCommand();
command.CommandText = insert;
command.ExecuteNonQuery();
}
}
}
Таким образом, запрос sqlite будет меняться в зависимости от выбранного элемента поля со списком, тем самым изменяя и диаграмму.
Когда я запускаю этот код, он работает так, как задумано, но только один раз, когда я выбираю другой элемент в поле со списком, все остается прежним.
Буду очень признателен за вашу помощь
Моя программа состоит из диаграммы из библиотеки LiveCharts, которая привязана к запросу sqlite, этот запрос sqlite будет меняться в зависимости от выбранного элемента поля со списком, я хочу, чтобы это изменение в запросе sqlite также означало изменение диаграмма. вот мой код: cs: [code]using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using LiveCharts; using LiveCharts.Wpf; using System.Data; using System.Data.SQLite; using INotify;
namespace example { /// /// Lógica de interacción para Charts.xaml /// public partial class Charts : Window {
string dow = ""; public Charts() { InitializeComponent();
comboBox.SelectedIndex = 1;
}
public SeriesCollection SeriesCollection { get; set; } public string[] Labels { get; set; } public Func Formatter { get; set; }
private void ComboBox_DropDownClosed_1(object sender, EventArgs e) { ComboBoxItem cbi = (ComboBoxItem)comboBox.SelectedItem; dow = cbi.Content.ToString(); label.Content = dow; List amount = new List(); List product = new List();
DatabaseHelper dbh = new DatabaseHelper(); SQLiteConnection connection; connection = dbh.Createconnection(); String loadchart = "SELECT product, count(product) AS Amount FROM table_productsold WHERE DATE like '" + dow + "%' GROUP BY product; ";
while (reader.Read()) { amount.Add(Convert.ToInt32(reader["Amount"])); product.Add(reader["product"].ToString()); }
SeriesCollection = new SeriesCollection { new ColumnSeries { Title = "2015", Values = new ChartValues(amount) } };
//adding series will update and animate the chart automatically
Labels = product.ToArray(); Formatter = value => value.ToString("N");
DataContext = this;
connection.Close(); dow = ""; } } [/code]
xaml: [code]
[/code]
Мой помощник по sqlite: [code]using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SQLite; using System.Windows;
namespace example { class DatabaseHelper { public SQLiteConnection Createconnection() { SQLiteConnection connection; connection = new SQLiteConnection(@"Data Source=C:\Users\Alan\source\repos\example\example\example.db;New=True;Compress=True;" );
public void createtablelogs(SQLiteConnection conn) { SQLiteCommand command; string ctl = @"CREATE TABLE IF NOT EXISTS table_productsold(product TEXT NOT NULL, price int, DATE TEXT NOT NULL);"; command = conn.CreateCommand(); command.CommandText = ctl; command.ExecuteNonQuery(); }
public void insertProductsold(SQLiteConnection conn, string products, int prices) { SQLiteCommand command; string insert = "INSERT INTO table_productsold VALUES('"+products+"',"+prices+", case cast(strftime('%w', 'now', 'localtime')as integer)" + "when 0 then 'Dom' ||' '|| strftime('%Y/%m/%d, %H:%M', 'now', 'localtime') " + "when 1 then 'Lun' ||' '|| strftime('%Y/%m/%d, %H:%M', 'now', 'localtime')" + "when 2 then 'Mar' ||' '|| strftime('%Y/%m/%d, %H:%M', 'now', 'localtime')" + "when 3 then 'Mie' ||' '|| strftime('%Y/%m/%d, %H:%M', 'now', 'localtime')" + "when 4 then 'Jue' ||' '|| strftime('%Y/%m/%d, %H:%M', 'now', 'localtime')" + "when 5 then 'Vie' ||' '|| strftime('%Y/%m/%d, %H:%M', 'now', 'localtime')" + "when 6 then 'Sab' ||' '|| strftime('%Y/%m/%d, %H:%M', 'now', 'localtime')" + "end);"; command = conn.CreateCommand();
} } [/code] Таким образом, запрос sqlite будет меняться в зависимости от выбранного элемента поля со списком, тем самым изменяя и диаграмму. Когда я запускаю этот код, он работает так, как задумано, но только один раз, когда я выбираю другой элемент в поле со списком, все остается прежним. Буду очень признателен за вашу помощь