How to plot a histogram with strings in anylogic - anylogic

i have an array with some random sequence
for example : long[] X = {23,1,4,2,.............,4,2,1,3,..........};
in data base i have the names for each numbers(names in the image)
i want to plot a histogram for (23,4);(1,2);(4,2);(2,3)............from the sequence in X[].
but I need something like this
I am not sure how to do it, as the data set only allows the type double, how can I add strings.
long[] X = Disassemblysequence.getDisassemblySequence(); //random sequence
String[] Components = f_Components(); //list of Components from data base
for(int i = 0; i<C; i++){
String A = Components[(int)(X[i] -1)];
double EOL = (double)X[i+C];
String Status = "";
//double Com = (double) X[i];
//double S = (double) X[i+C];
if (EOL == 0){
Status = "Not Removed";
}
else if(EOL == 2){
Status = "Reuse";
}
else if(EOL == 3){
Status = "Remanufacture";
}
else if(EOL == 4){
Status = "Recycle";
}
d_Components.add(A,EOL);
}

It appears that you don't want a histogram per see, since a histogram will calculate some of the values for you, which you already have calculated. Rather you simply need to display them in a bar chart.
You can add bars to a bar chart using standard code see example below
And here is the output
If you want to have custom x-axis labels you need to not show the legend and create your own - but this can be a separate question

Related

Using zoomCallback, how can I "snap" the zoom to existing x values?

I'm trying to use the zoomCallback function to set up interaction between my dygrpahs chart and a map chart. My x values are timestamps in seconds but since the sample rate is about 100Hz the timestamps are stored as float numbers.
The goal is that when dygraphs chart is zoomed in, the new x1 and x2 will be used to extract a piece of GPS track (lat, lng points). The extracted track will be used to re-fit the map boundaries - this will look like a "zoom in" on the map chart.
In my dygraphs options I specified the callback:
zoomCallback: function(x1,x2) {
let x1Index = graphHolder.getRowForX(x1);
let x2Index = graphHolder.getRowForX(x2);
// further code
}
But it looks like the zoom is not "snapped" to existing timestamp points so both x1Index and x2Index are null. Only when I zoom out, they'll correctly point to row 0 and the last row of data.
So the question is - is there a way to make the zoom snap only to the nearest existing x value so the row number can be returned? Or, is there an alternative to do what I want?
Thanks for any insights!
You can access the x-axis values via g.getValue(row, 0). From this you can either do a linear scan to find the first row in the range or (fancier but faster) use a binary search.
Here's a way to do the linear scan:
const [x1, x2] = g.xAxisRange();
let letRow = null, highRow = null;
for (let i = 0; i < g.numRows(); i++) {
if (g.getValue(i, 0) >= x1) {
lowRow = i;
break;
}
}
for (let i = g.numRows() - 1; i >= 0; i--) {
if (g.getValue(i, 0) <= x2) {
highRow = i;
break;
}
}
const dataX1 = g.getValue(lowRow, 0);
const dataX2 = g.getValue(highRow, 0);
For larger data sets you might want to do a binary search using something like lodash's _.sortedIndex.
Update Here's a binary search implementation. No promises about the exact behavior on the boundaries (i.e. whether it always returns indices that are inside the visible range or indices which contain the visible range).
function dygraphBinarySearch(g, x) {
let low = 0;
let high = g.numRows() - 1;
while (high > low) {
let i = Math.floor(low + (high - low) / 2);
const xi = g.getValue(i, 0);
if (xi < x) {
low = i + 1;
} else if (xi > x) {
high = i - 1;
} else {
return i;
}
}
return low;
}
function getVisibleDataRange(g) {
const [x1, x2] = g.xAxisRange();
let lowI = dygraphBinarySearch(g, x1);
let highI = dygraphBinarySearch(g, x2);
return [lowI, highI];
}

ILNumerics ILSurface Adjust Z values

Is there a way to manipulate the range of Z values for a Surface plot in a way that can preserve the original values so I can create a range slider with a min and max values from the GetLimits() method and then update the data array Z values so I can set new limits but move the slides back and forth to adjust the min/max Z value and see the plot adjust as I do it?
Given this code
ILArray<float> tempArray = ILMath.tosingle(myDoubleArray);
dataArray.a = tempArray;
var plotCube = ilPanel1.Scene.First<ILPlotCube>();
var surface = plotCube.First<ILFastSurface>();
surface.Update(Z: dataArray, colormap: new ILColormap(ILColormaps.ILNumerics));
ilPanel1.Refresh();
The MinValue and MaxValue controls are initialized like this.
float maxZ, minZ;
dataArray.GetLimits(out minZ, out maxZ);
var zRange = maxZ - minZ;
MinValue.Maximum = (decimal)maxZ;
MinValue.Minimum = (decimal)minZ;
MinValue.Value = (decimal)minZ;
MaxValue.Maximum = (decimal)maxZ;
MaxValue.Minimum = (decimal)minZ;
MaxValue.Value = (decimal)maxZ;
I want to be able to manipulate the Z values in the array like this
dataArray[dataArray < (float)MinValue.Value] = (float)MinValue.Value;
dataArray[dataArray > (float)MaxValue.Value] = (float)MaxValue.Value;
var plotCube = ilPanel1.Scene.First<ILPlotCube>();
var surface = plotCube.First<ILFastSurface>();
surface.Update(Z: dataArray, colormap: new ILColormap(ILColormaps.ILNumerics));
ilPanel1.Refresh();
The issue is that dataArray is being changed with new min/max values. How can I restore dataArray if you want to change back to a larger min/max? Do I just clone dataArray and use that to change the plot? Or is there a feature of ILArray that tracks changes and can restore the array?
The solution to this problem is to use a temporary array and clone the original array and then normalize the temporary array and update the surface with it.
float min = surface.GetRangeMinValue(AxisNames.CAxis) + minOffs;
float max = surface.GetRangeMaxValue(AxisNames.CAxis) + maxOffs;
ILArray<float> tempArray = _dataArray.C;
tempArray[tempArray < min] = min;
tempArray[tempArray > max] = max;
surface.Update(Z: tempArray);
_dataArray is a static property loaded once with the original data. Any updates are done with the code above

I have 2 Achartengine XYSeries problems

I’m having 2 problems with a simple XYSeries line graph.
When the absolute value of the difference between the last plotted point and the next plotted point is less than 11 the label on the next plotted point goes missing. I want all the labels to display.
I have some (not all) missing vertical grid lines and don’t see why. I want a vertical grid line for every XY coordinate.
Thanks for the help. Here's the code.
String glucoseLegendText = getString(R.string.glucose_legend_text);
XYSeries series = new XYSeries(glucoseLegendText);
datasource = new HistoryDataSource(this);
datasource.open();
Cursor c = datasource.getQuery();
c.moveToFirst();
int cnt = c.getCount();
int minValue = 0;
int maxValue = 0;
for (int i = 0; i < cnt; i++) {
int glucoseValue = c.getInt(2);
series.add(i, glucoseValue);
if (i == 0 || glucoseValue < minValue)
minValue = glucoseValue;
if (glucoseValue > maxValue)
maxValue = glucoseValue;
c.moveToNext();
}
datasource.close();
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series);
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(Color.BLUE);
renderer.setPointStyle(PointStyle.CIRCLE);
renderer.setFillPoints(true);
renderer.setLineWidth(3);
renderer.setDisplayChartValues(true);
renderer.setChartValuesTextSize(15);
renderer.setChartValuesTextAlign(Align.LEFT);
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
mRenderer.addSeriesRenderer(renderer);
mRenderer.setShowGrid(true);
mRenderer.setGridColor(Color.BLACK);
mRenderer.setXLabels(cnt); // to control number of grid lines
mRenderer.setYLabels(cnt); // to control number of grid lines
mRenderer.setXLabelsColor(Color.YELLOW);
mRenderer.setPointSize(5);
mRenderer.setYLabelsAlign(Align.RIGHT);
mRenderer.setLegendTextSize(15);
mRenderer.setZoomButtonsVisible(true);
mRenderer.setYAxisMin(minValue - 2); // buffer bottom
mRenderer.setYAxisMax(maxValue + 3); // buffer top
// Populate the X labels with the dates and times
c.moveToFirst();
for (int d = 0; d < cnt; d++) {
timeStamp = c.getString(1);
date = timeStamp.substring(0, 5);
time = timeStamp.substring(9, 14);
if (date.equals(dateLast)) {
mRenderer.addXTextLabel(d, "\n" + time);
} else
mRenderer.addXTextLabel(d, "\n" + time + "\n" + date);
dateLast = date;
c.moveToNext();
}
c.close();
GraphicalView gview = ChartFactory.getTimeChartView(this, dataset,
mRenderer, "");
LinearLayout layout = (LinearLayout) findViewById(R.id.Chart);
layout.addView(gview);
For the first question, you can control the labels display using
renderer.setXLabels(approximateNumberOfLabels);
For the second question, the grid lines are displayed along the labels.

GWT Highcharts : Displaying 'Date' values on X-Axis

I am using GWT Highcharts to draw a bar-graph.
The values for both X and Y axis are coming from the MySQL database.
The chart will display a graph having vc values on Y-Axis and corresponding sampleno (i.e. Date) on X-Axis.
Following is the code of loop that fills the series.
for(int i = 0; i < stats.size(); i++)
{
long sample = Long.parseLong(stats.get(i).getSampleno() + "");
long n = (sample*60000);
pointDate = new Date(n);
Point point = new Point( /* pointDate?? */ ,stats.get(i).getVc());
serPt.addPoint(point);
}
The sampleno is some timestamp which I am converting into Date.
Now, Could you guide me how can I display this date as values on x-axis.
An example on Moxie Group site does this, but the dates are in a fixed range there.
Please comment if any further explanation is needed.
I advice to use labels formatter http://api.highcharts.com/highcharts#xAxis.labels.formatter and then return value by using http://api.highcharts.com/highcharts#Highcharts.dateFormat()
labels: {
formatter: function() {
return Highcharts.dateFormat('%Y/%m/%d',this.value);
}
}

How to put show an outline for each chart series in a RadChart?

I'm trying to customize the appearance of an ASP Web Forms RadChart where the series have FillType = Solid while still allowing visibility of the lesser point values behind larger ones. What I really want is each series to have an outline placed on top of all the fills (basically the effect of having a ChartSeriesType.Line on top of a ChartSeriesType.Area).
I've experimented with setting LineAppearance.Shadow, color transparency, and combinations of those but it's still too difficult to discern background series values.
Is there not a simple way to turn on an outline for each series when using a solid fill? OR to set the transparency of the background fill only, not the edges?
Note the transparency in the above image. I'm afraid allowing more transparency improves the visibility of background values but results in a horrible collection of pastel colors.
I suggest you to go through telerik documentation page RadControls for ASP.NET AJAX Documentation -Styling Chart Elements and this nice example Creating RadChart Programmatically - more complex example.
Code snippet:
// Define chart and titleRadChart radChart = new RadChart();
radChart.ChartTitle.TextBlock.Text = "My RadChart";
radChart.ChartTitle.TextBlock.Appearance.TextProperties.Color = System.Drawing.Color.Blue;
// Define chart series
ChartSeries chartSeries = new ChartSeries();
chartSeries.Appearance.LabelAppearance.Visible = false;
chartSeries.Name = "GDP";
chartSeries.Type = ChartSeriesType.Line;
chartSeries.Appearance.LineSeriesAppearance.Color = System.Drawing.Color.BlueViolet;
// Define the items in the series
chartSeries.AddItem(1);
chartSeries.AddItem(1.5);
chartSeries.AddItem(2.0);
chartSeries.AddItem(2.5);
chartSeries.AddItem(3.5);
// visually enhance the datapoints
chartSeries.Appearance.PointMark.Dimensions.AutoSize = false;
chartSeries.Appearance.PointMark.Dimensions.Width = 5;
chartSeries.Appearance.PointMark.Dimensions.Height = 5;
chartSeries.Appearance.PointMark.FillStyle.MainColor = System.Drawing.Color.Black;
chartSeries.Appearance.PointMark.Visible = true;
// Define chart series
ChartSeries chartSeries2 = new ChartSeries();
chartSeries2.Appearance.LabelAppearance.Visible = false;
chartSeries2.Name = "GNP";
chartSeries2.Type = ChartSeriesType.Line;
chartSeries2.Appearance.LineSeriesAppearance.Color = System.Drawing.Color.Green;
// Define the items in the series
chartSeries2.AddItem(2);
chartSeries2.AddItem(3);
chartSeries2.AddItem(3.5);
chartSeries2.AddItem(4);
chartSeries2.AddItem(4.5);
// visually enhance the data points
chartSeries2.Appearance.PointMark.Dimensions.AutoSize = false;
chartSeries2.Appearance.PointMark.Dimensions.Width = 5;
chartSeries2.Appearance.PointMark.Dimensions.Height = 5;
chartSeries2.Appearance.PointMark.FillStyle.MainColor = System.Drawing.Color.Black;
chartSeries2.Appearance.PointMark.Visible = true;
// set the plot area gradient background fill
radChart.PlotArea.Appearance.FillStyle.FillType = Telerik.Charting.Styles.FillType.Gradient;
radChart.PlotArea.Appearance.FillStyle.MainColor = System.Drawing.Color.FromArgb(65, 201, 254);
radChart.PlotArea.Appearance.FillStyle.SecondColor = System.Drawing.Color.FromArgb(0, 107, 186);
// Set text and line for X axis
radChart.PlotArea.XAxis.AxisLabel.TextBlock.Text = "Years";
radChart.PlotArea.XAxis.AxisLabel.TextBlock.Appearance.TextProperties.Color = System.Drawing.Color.Red;
radChart.PlotArea.XAxis.Appearance.Width = 3;
radChart.PlotArea.XAxis.Appearance.Color = System.Drawing.Color.Red;
// Set text and line for Y axis
radChart.PlotArea.YAxis.AxisLabel.TextBlock.Text = "%";
radChart.PlotArea.YAxis.AxisLabel.TextBlock.Appearance.TextProperties.Color = System.Drawing.Color.Red;
radChart.PlotArea.YAxis.Appearance.Width = 3;
radChart.PlotArea.YAxis.Appearance.Color = System.Drawing.Color.Red;
// Add the series to the chart, chart to page.radChart.Series.Add(chartSeries);radChart.Series.Add(chartSeries2);this.Page.Controls.Add(radChart)
Hope this help..