I am using LiveCharts 0.9.7 and 1.2.9 Geared Version on WPF to show population data on ColumnSeries.
Here is my scenario: At the beginning, I am filling Columns with Blue. I want to change the colour of a single Column's Value on runtime.
I tried to reach a single value with SeriesCollection[0].Values[0] but, there no Fill or Colour option, it is just Double.
I also tried to cast SeriesCollection[0] to ColumnSeries but, I couldn't achieve the result. Is it possible to update a single value's colour on runtime?
public SeriesCollection SeriesCollection { get; set; } = new SeriesCollection
{
new ColumnSeries
{
Title = "Population of Bodrum",
Values = new ChartValues<double> { 1500, 2500, 3700, 2000, 1000},
Fill = Brushes.Blue
}
};
You can specify a configuration by assigning a CartesianMapper to ColumnSeries.Configuration.
The following example changes the color of the third column of your given chart from blue to red:
public class ChartDataModel
{
public ChartDataModel()
{
this.BlueSeries = new ColumnSeries()
{
Title = "Population of Bodrum",
Values = new ChartValues<double> { 1500, 2500, 3700, 2000, 1000 },
Fill = Brushes.Blue
};
this.SeriesCollection = new SeriesCollection() { this.BlueSeries };
}
private void ChangeThirdChartPointColorToRed()
{
CartesianMapper<double> mapper = Mappers.Xy<double>()
.X((value, index) => index)
.Y(value => value)
.Fill((value, index) => index == 2 ? Brushes.Red : Brushes.Blue);
// Dynamically set the third chart point color to red
this.BlueSeries.Configuration = mapper;
}
// The actual chart data source
public SeriesCollection SeriesCollection { get; set; }
private ColumnSeries BlueSeries { get; set; }
}
You can also use the CartesianMapper to change the color of a point according to its y value by specifying a corresponding predicate. To draw all values that exceed a value of 2,000 red you could use the following mapper:
CartesianMapper<double> mapper = Mappers.Xy<double>()
.X((value, index) => index)
.Y(value => value)
.Fill((value, index) => value > 2000 ? Brushes.Red : Brushes.Blue);
Related
I have this Object:
class Car
{
public int Id { get; set; }
public string Name { get; set; }
public Color Color { get; set; }
}
public enum Color
{
Red = 1,
Blue = 2,
Pink = 3,
Orange = 4,
}
How to create a linq query if I want take objects which have Red and Blue values:
query = query.Where(at => at.Color == Color.Red + Color.Blue);
If I take you at face value, and you want cars to be able to have more than one colour then you need to change your enum to use the Flags attribute.
Like this:
[Flags]
public enum Color
{
Red = 1,
Blue = 2,
Pink = 4,
Orange = 8,
}
Now I can write this code:
var cars = new []
{
new Car() { Name = "Red & Orange", Color = Color.Red | Color.Orange },
new Car() { Name = "Red & Blue", Color = Color.Red | Color.Blue },
};
var query = cars.Where(at => at.Color == (Color.Red | Color.Blue));
That, indeed, returns just the "Red & Blue" car.
However, if you meant or rather than and then you don't need to change your enum and the following is what you need:
query = query.Where(at => at.Color == Color.Red || at.Color == Color.Blue);
Either you can make the query with || or operator
query = query.Where(at => at.Color == Color.Red
|| at.Color == Color.Blue);
Or create an Color array to check whether the value is within the array.
query = query.Where(at => (new Color[] { Color.Red, Color.Blue }).Contains(at.Color));
I am returning my query value in array like,
public IEnumerable<ItemTable> SearchItem(string itemName)
{
return (from i in
_connection.Table<ItemTable>()
where i.ItemName.StartsWith(itemName)
select i).ToArray();
}
Can anyone tell me how to get this array value when i call this function means how to get this result to show on screen on label's text. Please Reply
Here is a sample;
public IEnumerable<ItemTable> SearchItem(string itemName)
{
return (from i in
_connection.Table<ItemTable>()
where i.ItemName.StartsWith(itemName)
select i).ToArray();
}
private string DisplayItemsInLabel(string itemName)
{
var searchedItems = this.SearchItem(itemName); // get items using above method that you have written already.
// get name and price
var displayableItems = searchedItems.Select(i => string.Format("Name :{0}, Price :{1}", i.Name, i.Price));
// create a formatted string using name and the price. so that we can display it in a label.
return string.Join(Environment.NewLine, displayableItems);
}
public LabelPage()
{
InitializeComponent();
var layout = new StackLayout { Padding = new Thickness(5, 10) };
this.Content = layout;
//display contents in a label
var label = new Label { Text = DisplayItemsInLabel("MyItem"), TextColor = Color.FromHex("#77d065"), FontSize = 20 };
layout.Children.Add(label);
}
I have few tables, using Entity Framework 6. My goal is to bind class table1 to ComboBox Value Member
ComboBox DataSource is:
ComboBoxBasicDB[] statType = new ComboBoxBasicDB[] {
new ComboBoxBasicDB { Text = "A1", Value = 0 },
new ComboBoxBasicDB { Text = "A2", Value = 1 },
new ComboBoxBasicDB { Text = "A3", Value = 2 },
new ComboBoxBasicDB { Text = "A4", Value = 4 },
new ComboBoxBasicDB { Text = "B12", Value = 12 },
new ComboBoxBasicDB { Text = "B13", Value = 13 },
new ComboBoxBasicDB { Text = "B14", Value = 14 }
};
statBS.DataSource = statType; // statBS == BindingSource, configured throught VS designer, comboBox.DataSource = statBS, comboBox.ValueMember = Value, comboBox.DisplayMember = Text
table1 contains property called ex. Value1 which contains one of these (0, 1, 2, 4, 12, 13, 14)
What am I trying to do is to load from DB row and use something like this on TextBox:
textBox.DataBindings.Add("Text", binding, "Name");
which works perfectly
I tried something like this:
comboBox.DataBindings.Add("SelectedValue", binding, "Value1");
but it not working, nothing is selected after query. textBox bind successfully
I used SelectedIndex but there is going one problem, and that is value above 7, because there are 7 items in statType not 14.
I hope you understand what am I trying to do :/
I thought I could do that throught comboBox.DataManager but its private
Thanks for any ideas.
So solution is custom implementation, in mentioned DataBindings change SelectedValue to SelectedItemValue
Implementation:
public class ComboBoxBasic : ComboBox
{
bool diffTextColor = false;
public ComboBoxBasic()
{
}
public object SelectedItemValue
{
get
{
return (SelectedItem as ComboBoxBasicDB).Value;
}
set
{
for(int i = 0; i < Items.Count; i++)
{
ComboBoxBasicDB item = Items[i] as ComboBoxBasicDB;
if(item.Value.ToString() == value.ToString())
{
SelectedIndex = i;
break;
}
}
}
}
public bool DifferentTextColor
{
get { return diffTextColor; }
set
{
diffTextColor = value;
if (diffTextColor)
{
DrawItem += ComboBoxBasic_DrawItem;
DrawMode = DrawMode.OwnerDrawFixed;
}
else
DrawItem -= ComboBoxBasic_DrawItem;
}
}
void ComboBoxBasic_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
if (e.State == DrawItemState.Focus)
e.DrawFocusRectangle();
Brush brush = new SolidBrush((sender as Control).ForeColor);
ComboBoxBasicDB item = (sender as ComboBoxBasic).Items[e.Index] as ComboBoxBasicDB;
if (item.ForeColor != Brushes.Black)
brush = item.ForeColor;
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
e.Graphics.DrawString(item.Text, (sender as Control).Font, brush, e.Bounds.X, e.Bounds.Y);
}
}
Also there is custom DrawItem if its enabled by DifferentTextColor
I am experiencing trouble getting what I am looking for, in respect to setting series for highcharts. I want to use my table in my database to post the number for the y-axis.
So in my table I have the properties, ID, TeamName, TotalWins.
I only have 2 records
ID = 1, TeamName = Boston Red Sox, TotalWins? = 0 nullable because the MLB season hasn't started yet
ID = 2, TeamName = Baltimore Orioles, TotalWins? = 0
Here is my ActionResult for my Chart:
public ActionResult Chart()
{
Highcharts chart = new Highcharts("chart")
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Pie })
.SetTitle(new Title { Text = "Who Has more Wins?" })
.SetSubtitle(new Subtitle { Text = "Source: Sportscenter" })
.SetXAxis(new XAxis
{
Categories = new[] { "Boston Red Sox", "Baltimore Orioles" },
Title = new XAxisTitle { Text = "Teams" }
})
.SetYAxis(new YAxis
{
Min = 0,
Title = new YAxisTitle
{
Text = "Wins (Game)",
Align = AxisTitleAligns.High
}
})
.SetTooltip(new Tooltip { Formatter = "function() { return ''+ this.series.name +': '+ this.y +' millions'; }" })
.SetPlotOptions(new PlotOptions
{
Bar = new PlotOptionsBar
{
DataLabels = new PlotOptionsBarDataLabels { Enabled = true }
}
})
.SetLegend(new Legend
{
Layout = Layouts.Horizontal,
Align = HorizontalAligns.Right,
VerticalAlign = VerticalAligns.Top,
X = -100,
Y = 100,
Floating = true,
BorderWidth = 1,
BackgroundColor = new BackColorOrGradient(ColorTranslator.FromHtml("#FFFFFF")),
Shadow = true
})
.SetCredits(new Credits { Enabled = false })
.SetSeries(new[]
{
new Series { Data = new Data(new object[] { db.Teams.Where(x => x.TeamName == "Boston Red Sox").Count(x => x.TotalWins) /*where the issue is */ }) },
});
return View(chart);
}
cannot implicitly convert type 'int?' to 'bool'
How do I set this lambda expression so that it retrieves the total wins for the boston red sox and then again for the baltimore orioles?
The problem is here:
.Count(x => x.TotalWins)
Count either takes no arguments (in which case it returns the total count of the results of the preceding query), or a lambda that returns a boolean expression, in which case it returns a count of items that meet the criteria.
Do you mean .Sum(x => x.TotalWins)?
I am using Dotnet Highchart with MVC3
I am currently working with a diagram that looks like this:
I am trying to modify my code so I can change color on the bars depending on what number they have. I also wonder how I can remove the button "Snittbetyg" as you see can on the image.
This is my code:
public ActionResult OfficeStatistic()
{
{
Highcharts chart1 = new Highcharts("chart1")
.SetXAxis(new XAxis { Categories = new[] { "Ödmjukhet", "Engagemang", "Kompetens", "Lönsamhet" } })
.SetYAxis(new YAxis { Title = new YAxisTitle { Text = "Betygskalan" } })
.SetSeries(new Series { Data = new Data(new object[] { 1, 8, 9, 6 }), Name = "Snittbetyg" })
.SetTitle(new Title { Text = "Örebro Statistik" })
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Column });
return View(chart1);
}
}
Any kind of help is appreciated.
Thanks in advance!
I haven't used Highchart but you can download examples from their codeplex page. It looks like both of your requirements can be achieved easily.
Remove the "Snittbetyg" button
Disable the legend:
.SetLegend(new Legend { Enabled = false });
Add Colours
For the series data use points instead of just the numbers:
Data data = new Data(new[]
{
new Point { Y = 1, Color = System.Drawing.Color.Red },
new Point { Y = 8, Color = System.Drawing.Color.Blue },
new Point { Y = 9, Color = System.Drawing.Color.Green },
new Point { Y = 6, Color = System.Drawing.Color.Black }
});
Highcharts chart1 = new Highcharts("chart1")
.SetXAxis(new XAxis { Categories = new[] { "Ödmjukhet", "Engagemang", "Kompetens", "Lönsamhet" } })
.SetYAxis(new YAxis { Title = new YAxisTitle { Text = "Betygskalan" } })
.SetSeries(new Series { Data = data, Name = "Snittbetyg" })
.SetTitle(new Title { Text = "Örebro Statistik" })
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Column })
.SetLegend(new Legend { Enabled = false });
There doesn't seem to be a built in way to make highchart automatically colour the bar based on the y-value. I believe you would have to pick the colour yourself, e.g:
private System.Drawing.Color GetBarColour(int value)
{
if (value < 5) return System.Drawing.Color.Red;
if (value > 7) return System.Drawing.Color.Green;
return System.Drawing.Color.Orange;
}
public ActionResult OfficeStatistic()
{
{
var dataItems = new[] {1, 8, 9, 6};
Data data = new Data(
dataItems.Select(y => new Point {Color = GetBarColour(y), Y = y}).ToArray()
);
Highcharts chart1 = new Highcharts("chart1")
.SetXAxis(new XAxis { Categories = new[] { "Ödmjukhet", "Engagemang", "Kompetens", "Lönsamhet" } })
.SetYAxis(new YAxis { Title = new YAxisTitle { Text = "Betygskalan" } })
.SetSeries(new Series { Data = data, Name = "Snittbetyg" })
.SetTitle(new Title { Text = "Örebro Statistik" })
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Column })
.SetLegend(new Legend { Enabled = false });
First, define a Tuple list first item is for color and second item point value
List<Tuple<string, Object>> dataItems = new List<Tuple<string, Object>>();
i am passing value with swtich it is not neccessary
SqlDataReader reader = myComm.ExecuteReader();
if (reader.HasRows)
{
string colorName ="";
while (reader.Read())
{
switch ((string)reader.GetValue(1))
{
case "Total Employee(s)":
colorName = "Blue";
break;
case "Present":
colorName = "Green";
break;
case "Late":
case"Absent":
case "During Less":
case "Early Going":
colorName = "Red";
break;
case "Leave":
colorName = "Orange";
break;
default:
colorName = "Gray";
break;
}
dataItems.Add(new Tuple<string, Object>(colorName, reader.GetValue(2)));
}
Now, Finally add Data into series object
new Series{
Name = "Employees",
Data = new Data(
dataItems.Select(y => new Point {
Color = System.Drawing.Color.FromName(y.Item1),
Y = (int)y.Item2 }).ToArray()
)
}