How to possible Xamarin Forms Vertical Bar Charts? - charts

In my xamarin forms application, I am using the Oxyplot bar charts. Here is the code, This is work for me in the condition for horizontal bars, How it is possible for creating for vertical graphs? Is there any other plugin for bar charts?

The key is to use a ColumnSeries with ColumnItem objects. This should render as a bar chart that has vertically oriented bars. If you create a new ContentPage and paste the code below into it you can see it in action. I attached an image below the code sample detailing how it looks on iOS.
public partial class DemoPage : ContentPage
{
public DemoPage()
{
InitializeComponent();
var model = new PlotModel { Title = "Cake Type Popularity" };
// generate a random percentage distribution between the 5
//cake-types (see axis below)
var rand = new Random();
double[] cakePopularity = new double[5];
for (int i = 0; i < 5; ++i)
{
cakePopularity[i] = rand.NextDouble();
}
var sum = cakePopularity.Sum();
var barSeries = new ColumnSeries
{
ItemsSource = new List<ColumnItem>(new[]
{
new ColumnItem{ Value = (cakePopularity[0] / sum * 100) },
new ColumnItem{ Value = (cakePopularity[1] / sum * 100) },
new ColumnItem{ Value = (cakePopularity[2] / sum * 100) },
new ColumnItem{ Value = (cakePopularity[3] / sum * 100) },
new ColumnItem{ Value = (cakePopularity[4] / sum * 100) }
}),
LabelPlacement = LabelPlacement.Inside,
LabelFormatString = "{0:.00}%"
};
model.Series.Add(barSeries);
model.Axes.Add(new CategoryAxis
{
Position = AxisPosition.Bottom,
Key = "CakeAxis",
ItemsSource = new[]
{
"A",
"B",
"C",
"D",
"E"
}
});
var grid = new Grid();
grid.Children.Add(new PlotView
{
Model = model,
VerticalOptions = LayoutOptions.Center,
HeightRequest = 200,
HorizontalOptions = LayoutOptions.Fill,
});
Content = grid;
}
}
This should render a graph like this:

Related

How to prevent my Image from scaling down?

I am using iText 7.2.1.
I create two images in the following code. They are two rectangles, created from PdfFormXObject. The first is 100 x 100, and the second is 1000 x 100. They have the same stroke width. I have SetAutoScale(false) for both images.
using iText.Kernel.Colors;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Kernel.Pdf.Xobject;
namespace iTextTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var writer = new PdfWriter("test.pdf");
var pdfdoc = new PdfDocument(writer);
var doc = new Document(pdfdoc);
// 100 x 100 rectangle
var rect1 = new iText.Kernel.Geom.Rectangle(0, 0, 100, 100);
var xobj1 = new PdfFormXObject(rect1);
var canvas1 = new PdfCanvas(xobj1, pdfdoc);
canvas1.SetStrokeColor(ColorConstants.RED);
canvas1.SetLineWidth(5);
canvas1.Rectangle(rect1);
canvas1.Stroke();
var img1 = new iText.Layout.Element.Image(xobj1).SetAutoScale(false);
doc.Add(img1);
// 1000 x 100 rectangle
var rect2 = new iText.Kernel.Geom.Rectangle(0, 0, 1000, 100);
var xobj2 = new PdfFormXObject(rect2);
var canvas2 = new PdfCanvas(xobj2, pdfdoc);
canvas2.SetStrokeColor(ColorConstants.RED);
canvas2.SetLineWidth(5);
canvas2.Rectangle(rect2);
canvas2.Stroke();
var img2 = new iText.Layout.Element.Image(xobj2).SetAutoScale(false);
doc.Add(img2);
doc.Close();
pdfdoc.Close();
writer.Close();
MessageBox.Show("OK");
}
}
}
When I add them into the document, apparently, the first image is of the original size, but the second image is scaled down.
I want the images to not scale at all even there isn't enough space. How to do this?

How to add buttons to a grid WP8.1 MVVM Light?

I'm having trouble building a dashboard in WP8.1.
I'm using the MVVM Light toolkit.
I'm looking after this outcome:
screenshot of dashboard
I can currently build a dashboard but only with one object per row.
I've searched a lot but haven't found anything to get the desired effect.
Here's the code of the dashboard done programatically:
int i = 0;
Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition());
for (int ct = 0; ct < 3; ct++)
grid.ColumnDefinitions.Add(new ColumnDefinition());
StackPanel stackPanel = new StackPanel();
stackPanel.Name = "stackPanel";
stackPanel.Background = new SolidColorBrush(new Windows.UI.Color());
foreach (var flow in dashboard.DashboardFlows)
{
if (flow.Flow.IsInstalled)
{
//if next object should change line
if (i == 3)
{
stackPanel.Children.Add(grid);
grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition());
for (int ct = 0; ct < 3; ct++)
grid.ColumnDefinitions.Add(new ColumnDefinition());
i = 0;
}
Button button = new Button();
if (flow.Flow.Type.Equals("COMPNEWS"))
{
button.Click += delegate { Frame.Navigate(typeof(NewsPage), flow); };
}
//button stackpanel
StackPanel stack = new StackPanel();
//Button image
Image buttonImage = new Image();
if (!String.IsNullOrEmpty(flow.Flow.IconUrl) || !String.IsNullOrWhiteSpace(flow.Flow.IconUrl))
buttonImage.Source = new BitmapImage(new Uri(flow.Flow.IconUrl));
buttonImage.Height = 45;
buttonImage.VerticalAlignment = VerticalAlignment.Top;
stack.Children.Add(buttonImage);
//Button name
TextBlock buttonName = new TextBlock();
buttonName.Text = flow.Flow.Name;
buttonName.FontSize = 12;
buttonName.TextWrapping = TextWrapping.Wrap;
stack.Children.Add(buttonName);
button.Content = stack;
button.Margin = new Thickness(5, 0, 5, 0);
button.SetValue(Grid.ColumnProperty, i);
button.SetValue(Grid.RowProperty, 0);
button.HorizontalAlignment = HorizontalAlignment.Stretch;
button.VerticalAlignment = VerticalAlignment.Stretch;
button.Background = new SolidColorBrush(Color.FromArgb(130, 30, 30, 30));
grid.Children.Add(button);
i++;
}
}
scroll.Content = stackPanel;
Thank you in advance.

Add value to DataPoint by user

How to add values to DataPoint which to be plotted in case I do not know number of possible entries?
DataPoint[] db = new DataPoint[]{
new DataPoint(5,4),
new DataPoint(2,4),
new DataPoint(4,4),
new DataPoint(9,0),
new DataPoint(0,3)} ;
PointsGraphSeries<DataPoint> series = new PointsGraphSeries<DataPoint>(db) ;
Make a call to this method before you add DataPoint array to LineGraphSeries.
private void generateDataForGraph() {
int size = Amount.size();
values = new DataPoint[size];
for (int i=0; i<size; i++) {
Integer xi = Integer.parseInt(Dates.get(i));
Integer yi = Integer.parseInt(Amount.get(i));
DataPoint v = new DataPoint(xi, yi);
values[i] = v;
}
series = new LineGraphSeries<DataPoint>(values);
graph2.addSeries(series);
}

Kinect-Matlab: Get joint angle information

I am a beginner at kinect and I want to use it with matlab. I need joint angle information out of the skeleton data. What is the easiest way to do this?
Thanks.
I am not good with Mat lab, but try to use c# with Visual Studio. Use the SkeletonBasics-WPF in the Microsoft SDK´s as a template to add this code:
public class Angles
{
public double AngleBetweenTwoVectors(Vector3D vectorA, Vector3D vectorB)
{
double dotProduct;
vectorA.Normalize();
vectorB.Normalize();
dotProduct = Vector3D.DotProduct(vectorA, vectorB);
return (double)Math.Acos(dotProduct)/Math.PI*180;
}
public byte[] GetVector(Skeleton skeleton)
{
Vector3D ShoulderCenter = new Vector3D(skeleton.Joints[JointType.ShoulderCenter].Position.X, skeleton.Joints[JointType.ShoulderCenter].Position.Y, skeleton.Joints[JointType.ShoulderCenter].Position.Z);
Vector3D RightShoulder = new Vector3D(skeleton.Joints[JointType.ShoulderRight].Position.X, skeleton.Joints[JointType.ShoulderRight].Position.Y, skeleton.Joints[JointType.ShoulderRight].Position.Z);
Vector3D LeftShoulder = new Vector3D(skeleton.Joints[JointType.ShoulderLeft].Position.X, skeleton.Joints[JointType.ShoulderLeft].Position.Y, skeleton.Joints[JointType.ShoulderLeft].Position.Z);
Vector3D RightElbow = new Vector3D(skeleton.Joints[JointType.ElbowRight].Position.X, skeleton.Joints[JointType.ElbowRight].Position.Y, skeleton.Joints[JointType.ElbowRight].Position.Z);
Vector3D LeftElbow = new Vector3D(skeleton.Joints[JointType.ElbowLeft].Position.X, skeleton.Joints[JointType.ElbowLeft].Position.Y, skeleton.Joints[JointType.ElbowLeft].Position.Z);
Vector3D RightWrist = new Vector3D(skeleton.Joints[JointType.WristRight].Position.X, skeleton.Joints[JointType.WristRight].Position.Y, skeleton.Joints[JointType.WristRight].Position.Z);
Vector3D LeftWrist = new Vector3D(skeleton.Joints[JointType.WristLeft].Position.X, skeleton.Joints[JointType.WristLeft].Position.Y, skeleton.Joints[JointType.WristLeft].Position.Z);
Vector3D UpVector = new Vector3D(0.0, 1.0, 0.0);
double AngleRightElbow = AngleBetweenTwoVectors(RightElbow - RightShoulder, RightElbow - RightWrist);
double AngleRightShoulder = AngleBetweenTwoVectors(UpVector, RightShoulder - RightElbow);
double AngleLeftElbow = AngleBetweenTwoVectors(LeftElbow - LeftShoulder, LeftElbow - LeftWrist);
double AngleLeftShoulder = AngleBetweenTwoVectors(UpVector, LeftShoulder - LeftElbow);
byte[] Angles = {Convert.ToByte(AngleRightElbow), Convert.ToByte(AngleRightShoulder),Convert.ToByte(AngleLeftElbow),Convert.ToByte(AngleLeftShoulder)};
return Angles;
}
}
Insert this at the top. Call the GetVector() method here:
private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
{
Skeleton[] skeletons = new Skeleton[0];
using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
{
if (skeletonFrame != null)
{
skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
skeletonFrame.CopySkeletonDataTo(skeletons);
}
}
using (DrawingContext dc = this.drawingGroup.Open())
{
// Draw a transparent background to set the render size
dc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, RenderWidth, RenderHeight));
if (skeletons.Length != 0)
{
foreach (Skeleton skel in skeletons)
{
RenderClippedEdges(skel, dc);
if (skel.TrackingState == SkeletonTrackingState.Tracked)
{
this.DrawBonesAndJoints(skel, dc);
Angles MyAngles = new Angles();
byte[] ReadyAngles = MyAngles.GetVector(skel);
RightElbow.Text = ReadyAngles[0].ToString();
RightShoulder.Text = ReadyAngles[1].ToString();
LeftElbow.Text = ReadyAngles[2].ToString();
LeftShoulder.Text = ReadyAngles[3].ToString();
byte[] SequenceStart = {255};
if (ArduinoPort.IsOpen)
{
ArduinoPort.Write(SequenceStart,0,1);
ArduinoPort.Write(ReadyAngles, 0, 4);
}
}
else if (skel.TrackingState == SkeletonTrackingState.PositionOnly)
{
dc.DrawEllipse(
this.centerPointBrush,
null,
this.SkeletonPointToScreen(skel.Position),
BodyCenterThickness,
BodyCenterThickness);
}
}
}
// prevent drawing outside of our render area
this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, RenderWidth, RenderHeight));
}
}
As you can see I added 4 textboxes as I am calculating 4 Angles (RS, LF, RE, LE). These Angles are being passed to the textbox in my .xaml file. I hope this works for you.
You would need the Image Acquisition Toolbox, which includes support for Kinect including skeleton data.

OxyPlot Zero Line

We have a simple ColumnChart with positive and negative values.
There is no line across the chart though at the 0 line. How do we enable a zero line across?
See attached image
Assuming that you are using a LinearAxis for Y axis.
All you need to do is add to your LinearAxis.
plotModel.Axes.Add(new LinearAxis()
{
Title = "Percentage",
Position = AxisPosition.Left,
// Magic Happens here we add the extra grid line on our Y Axis at zero
ExtraGridlines = new Double[] { 0 }
});
Take a look at this works for me on Android haven't tested it on IOS:
public static PlotModel Withnegativevalues()
{
var plotModel1 = new PlotModel();
plotModel1.LegendBorderThickness = 0;
plotModel1.LegendOrientation = LegendOrientation.Horizontal;
plotModel1.LegendPlacement = LegendPlacement.Outside;
plotModel1.LegendPosition = LegendPosition.BottomCenter;
plotModel1.Title = "With negative values";
var categoryAxis1 = new CategoryAxis();
categoryAxis1.MinorStep = 1;
categoryAxis1.Labels.Add("Category A");
categoryAxis1.Labels.Add("Category B");
categoryAxis1.Labels.Add("Category C");
categoryAxis1.Labels.Add("Category D");
categoryAxis1.ActualLabels.Add("Category A");
categoryAxis1.ActualLabels.Add("Category B");
categoryAxis1.ActualLabels.Add("Category C");
categoryAxis1.ActualLabels.Add("Category D");
plotModel1.Axes.Add(categoryAxis1);
var linearAxis1 = new LinearAxis();
linearAxis1.MaximumPadding = 0.06;
linearAxis1.MinimumPadding = 0.06;
linearAxis1.ExtraGridlines = new Double[1];
linearAxis1.ExtraGridlines[0] = 0;
plotModel1.Axes.Add(linearAxis1);
var columnSeries1 = new ColumnSeries();
columnSeries1.StrokeThickness = 1;
columnSeries1.Title = "Series 1";
columnSeries1.Items.Add(new ColumnItem(25,-1,"OxyColors.Automatic"));
columnSeries1.Items.Add(new ColumnItem(137,-1,"OxyColors.Automatic"));
columnSeries1.Items.Add(new ColumnItem(18,-1,"OxyColors.Automatic"));
columnSeries1.Items.Add(new ColumnItem(40,-1,"OxyColors.Automatic"));
plotModel1.Series.Add(columnSeries1);
var columnSeries2 = new ColumnSeries();
columnSeries2.StrokeThickness = 1;
columnSeries2.Title = "Series 2";
columnSeries2.Items.Add(new ColumnItem(-12,-1,"OxyColors.Automatic"));
columnSeries2.Items.Add(new ColumnItem(-14,-1,"OxyColors.Automatic"));
columnSeries2.Items.Add(new ColumnItem(-120,-1,"OxyColors.Automatic"));
columnSeries2.Items.Add(new ColumnItem(-26,-1,"OxyColors.Automatic"));
plotModel1.Series.Add(columnSeries2);
var columnSeries3 = new ColumnSeries();
columnSeries3.StrokeThickness = 1;
columnSeries3.Title = "Series 3";
columnSeries3.Items.Add(new ColumnItem(21,-1,"OxyColors.Automatic"));
columnSeries3.Items.Add(new ColumnItem(8,-1,"OxyColors.Automatic"));
columnSeries3.Items.Add(new ColumnItem(48,-1,"OxyColors.Automatic"));
columnSeries3.Items.Add(new ColumnItem(3,-1,"OxyColors.Automatic"));
plotModel1.Series.Add(columnSeries3);
var columnSeries4 = new ColumnSeries();
columnSeries4.StrokeThickness = 1;
columnSeries4.Title = "Series 4";
columnSeries4.Items.Add(new ColumnItem(-8,-1,"OxyColors.Automatic"));
columnSeries4.Items.Add(new ColumnItem(-21,-1,"OxyColors.Automatic"));
columnSeries4.Items.Add(new ColumnItem(-3,-1,"OxyColors.Automatic"));
columnSeries4.Items.Add(new ColumnItem(-48,-1,"OxyColors.Automatic"));
plotModel1.Series.Add(columnSeries4);
return plotModel1;
}