Flat line instead wave - java

I want to plot a sine wave, but instead my application generates a flat line. When I use series with random from jchartfree example everything works fine. I also use debugger to check if values are good. Values are different than zero
public void createDataset() {
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series1 = new XYSeries("Object 1");
double A = 1;
double T = 1;
double Fs = 10;
double f = 200;
int rozmiar = (int) (T*Fs);
double[] x = new double[rozmiar];
for (int i = 0; i < rozmiar; i++)
{
x[i] = A * Math.sin(2 * Math.PI * f * i / Fs);
series1.add(i, x[i]);
}
dataset.addSeries(series1);
data = dataset;
}
//...
public void createChartPanel() {
//pWykres = new JPanel();
//if(java.util.Arrays.asList(getComponents()).contains(pWykres)){
//getContentPane().remove(pWykres);
//}
if(pWykres != null){
pWykres.removeAll();
pWykres.revalidate();
}
String chartTitle = "Objects Movement Chart";
String xAxisLabel = "X";
String yAxisLabel = "Y";
JFreeChart chart = ChartFactory.createXYLineChart(chartTitle,
xAxisLabel, yAxisLabel, dataset);
customizeChart(chart);
pWykres = new ChartPanel(chart);
getContentPane().add(pWykres, BorderLayout.CENTER);
setSize(620, 460);
//validate();
pWykres.repaint();
}
//endregion
//...
//region
private void customizeChart(JFreeChart chart) {
XYPlot plot = chart.getXYPlot();
XYSplineRenderer renderer;
renderer = new XYSplineRenderer();
renderer.setSeriesShapesVisible(0, false);
// sets paint color for each series
renderer.setSeriesPaint(0, Color.RED);
// sets thickness for series (using strokes)
renderer.setSeriesStroke(0, new BasicStroke(1.0f));
// sets paint color for plot outlines
//plot.setOutlinePaint(Color.BLUE);
//plot.setOutlineStroke(new BasicStroke(2.0f));
// sets renderer for lines
plot.setRenderer(renderer);
// sets plot background
plot.setBackgroundPaint(Color.WHITE);
// sets paint color for the grid lines
plot.setRangeGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.BLACK);
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.BLACK);
}

You are taking the sine of values that are always whole multiples of 2 * PI, so all of these values will be (approximately) zero, hence your graph will end up appearing flat unless it is scaled to show up these tiny values (which are just floating point errors).
x[i] = A * Math.sin(2 * Math.PI * f * i / Fs);
where A = 1 and f/Fs = 20 and i is integer
For example:
Math.sin(0) // 0.0
Math.sin(2 * Math.PI) // -2.4492935982947064E-16 (approximately zero)
Math.sin(4 * Math.PI) // -4.898587196589413E-16 (approximately zero)
To see the characteristic shape of a sine wave, you need to vary the input to the sin function by much smaller increments, e.g. pi/10 or less.

Related

This line of code is supposed to draw a graph. But it is not plotting anything. It is just showing the axes

I'm using jjoe64 graphview library
//Draws Graph
GraphView graph = findViewById(R.id.plot);
graph.setVisibility(View.VISIBLE);
DataPoint[] data = new DataPoint[term];
for (int i=0;i<term;i++){
data[i] = new DataPoint(i+1,P * java.lang.Math.pow(1 + (r) / 100, i));
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(data);
graph.addSeries(series);
The following code fixed it:
graph.removeAllSeries();
LineGraphSeries<DataPoint> series = new LineGraphSeries<>();
for (int i=1;i<=term;i++){
int x=i;
long y=Math.round(P * java.lang.Math.pow(1 + (r) / 100, i));
series.appendData(new DataPoint(x,y),true,term);
}
graph.addSeries(series);

MPAndroidChart BarChart - Vertical values inside of bars

I am using the MPAndroidChart library for the bar chart, In that, I have used chart.setDrawValueAboveBar(false) to set bar values inside of bars, now I want to display the values vertically inside of bars.
Please help..Thank you in advance.
first make this false
mChart.setDrawValueAboveBar(false);
then enable draw values for your data set
barDataSet.setDrawValues(true);
Since you need to rotate the text as well.. you have to implement a custom renderer for your chart. If you want to know how renderer works, check this answer
I have provided a sample implementation below. You can modify it to control the position of the text as it suits you.
Custom Renderer
public class BarChartCustomRenderer extends BarChartRenderer {
public BarChartCustomRenderer(BarDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
super(chart, animator, viewPortHandler);
}
public void drawValue(Canvas c, IValueFormatter formatter, float value, Entry entry, int dataSetIndex, float x, float y, int color) {
mValuePaint.setColor(color);
c.save();
c.rotate(90f, x, y);
Log.d("here", formatter.getFormattedValue(value, entry, dataSetIndex, mViewPortHandler) );
c.drawText(formatter.getFormattedValue(value, entry, dataSetIndex, mViewPortHandler), x, y, mValuePaint);
c.restore();
}
}
Chart Code
BarChart mChart = (BarChart) findViewById(R.id.barChart);
mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(false);
mChart.getDescription().setEnabled(false);
mChart.setDrawGridBackground(false);
//**add renderer**
BarChartCustomRenderer barChartCustomRenderer = new BarChartCustomRenderer(mChart, mChart.getAnimator(), mChart.getViewPortHandler());
mChart.setRenderer(barChartCustomRenderer);
XAxis xaxis = mChart.getXAxis();
xaxis.setDrawGridLines(false);
xaxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xaxis.setGranularity(0.5f);
xaxis.setGranularityEnabled(true);
xaxis.setDrawLabels(true);
xaxis.setDrawAxisLine(false);
YAxis yAxisLeft = mChart.getAxisLeft();
yAxisLeft.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
yAxisLeft.setDrawGridLines(false);
yAxisLeft.setDrawAxisLine(false);
yAxisLeft.setEnabled(false);
mChart.getAxisRight().setEnabled(false);
Legend legend = mChart.getLegend();
legend.setEnabled(false);
ArrayList<BarEntry> valueSet1 = new ArrayList<BarEntry>();
ArrayList<String> ylabels = new ArrayList<>();
for (int i = 0; i < 6; ++i) {
BarEntry entry = new BarEntry(i, (i + 1) * 2);
valueSet1.add(entry);
ylabels.add(" " + i);
}
List<IBarDataSet> dataSets = new ArrayList<>();
BarDataSet barDataSet = new BarDataSet(valueSet1, " ");
barDataSet.setColor(Color.CYAN);
barDataSet.setDrawValues(true);
dataSets.add(barDataSet);
BarData data = new BarData(dataSets);
data.setBarWidth(0.4f);
data.setValueTextSize(10f);
data.setValueTextColor(Color.BLACK);
mChart.setData(data);
mChart.setFitBars(true);
mChart.invalidate();
Result
class MyCustomRenderer(
chart: BarDataProvider, animator: ChartAnimator, viewPortHandler: ViewPortHandler
) : HorizontalBarChartRenderer(chart, animator, viewPortHandler) {
override fun drawValue(c: Canvas, valueText: String, x: Float, y: Float, color: Int) {
mValuePaint.color = color
val xPoint = Utils.convertDpToPixel(26f)
val yPoint = Utils.convertDpToPixel(16f)
c.drawText(valueText, xPoint , yPoint , mValuePaint)
}
}

Outline to blocks in XY Block renderer jfreechart

I am drawing a kind of heatmap using XYZ dataset and XY block renderer. The block's color is a function of the Z value and color is assigned using grayscale. i.e. the block with 0 Z value is assigned white color and one with maximum is assigned black color.
My grayscale goes from 0 to 100 (or more lets say). If the scale is this big, the blocks with count as 0 and 10 will have very little difference in color values. To understand, lets say the whole grid is divided in blocks. One block hasZ value of 100, one has 2 and all others are 0. Then, this block with 2 as Z value is not much visible because of very light shade.
I want to give outline to blocks of some color so that they are distinguishable. I tried with setBaseItemOutline() etc functions but none does this.
Any help on this?
Edit: Below
One of my classes is this:
public class BlockRenderer extends ApplicationFrame {
/**
* Constructs the demo application.
*
* #param title the frame title.
*/
public BlockRenderer(String title) {
super(title);
JPanel chartPanel = createDemoPanel();
//chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
/**
* Creates a chart for the specified dataset.
*
* #param dataset the dataset.
*
* #return A chart instance.
*/
private static JFreeChart createChart(XYZDataset dataset) {
NumberAxis xAxis = new NumberAxis("X");
xAxis.setLowerMargin(0.0);
xAxis.setUpperMargin(0.0);
NumberAxis yAxis = new NumberAxis("Y");
yAxis.setAutoRangeIncludesZero(false);
yAxis.setInverted(true);
yAxis.setLowerMargin(0.0);
yAxis.setUpperMargin(0.0);
yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
XYBlockRenderer renderer = new XYBlockRenderer();
CustomGrayPaintScale paintScale = new CustomGrayPaintScale(0,1000);
renderer.setPaintScale(paintScale);
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
plot.setBackgroundPaint(Color.lightGray);
plot.setAxisOffset(new RectangleInsets(5, 5, 5, 5));
JFreeChart chart = new JFreeChart("XYBlockChartDemo3", plot);
chart.removeLegend();
chart.setBackgroundPaint(Color.white);
SymbolAxis scaleAxis = new SymbolAxis(null, new String[] {"", "OK",
"Uncertain", "Bad"});
scaleAxis.setRange(0.5, 3.5);
scaleAxis.setPlot(new PiePlot());
scaleAxis.setGridBandsVisible(false);
PaintScaleLegend psl = new PaintScaleLegend(paintScale, scaleAxis);
psl.setAxisOffset(5.0);
psl.setPosition(RectangleEdge.BOTTOM);
psl.setMargin(new RectangleInsets(5, 5, 5, 5));
chart.addSubtitle(psl);
renderer.setBaseToolTipGenerator(new XYToolTipGenerator() {
#Override
public String generateToolTip(XYDataset dataset, int arg1, int arg2) {
// TODO Auto-generated method stub
XYZDataset xyzDataset = (XYZDataset)dataset;
return String.valueOf(xyzDataset.getZValue(arg1, arg2));
}
});
return chart;
}
/**
* Utility method called by createDataset().
*
* #param data the data array.
* #param c the column.
* #param r the row.
* #param value the value.
*/
private static void setValue(double[][] data,
int c, int r, double value) {
data[0][(r) * 10 + c] = c;
data[1][(r) * 10 + c] = r;
data[2][(r) * 10 + c] = value;
}
/**
* Creates a sample dataset.
*/
private static XYZDataset createDataset() {
double[] xvalues = new double[10*10];
double[] yvalues = new double[10*10];
double[] zvalues = new double[10*10];
double[][] data = new double[][] {xvalues, yvalues, zvalues};
// set the default z-value to zero throughout the data array.
int count [][] = new int[10][10];
for ( int i=0; i<10; i++ ) {
for ( int j=0; j<10; j++ ) {
count[i][j] = i*j;
if ( i==0 && j== 5 )
count[i][j] = 3;
}
}
for ( int i=0; i<10; i++ ) {
for ( int j=0; j<10; j++ ) {
setValue(data,j,i,count[i][j]);
}
}
DefaultXYZDataset dataset = new DefaultXYZDataset();
dataset.addSeries("Series 1", data);
System.out.println(dataset.getZValue(0, 1));
return dataset;
}
/**
* Creates a panel for the demo.
*
* #return A panel.
*/
public static JPanel createDemoPanel() {
return new ChartPanel(createChart(createDataset()));
}
/**
* Starting point for the demonstration application.
*
* #param args ignored.
*/
public static void main(String[] args) {
BlockRenderer demo = new BlockRenderer("Block Chart Demo 3");
//demo.pack();
demo.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
I have made CustomGrayPaintScale class to get white color for 0 Z values by over riding its getPaint(). If above class is run, we'll notice that the blocks are not much distinguishable. There is cell in the top row with value as 3 and all others in that row are 0. Because of my large range, its color value is not much different from its adjacent. So, I wanted something which can draw outline to these blocks. Also, I want to assign lets say blue color to some block items and others should have the paintscale on the basis of Z value only (Better if paint scale is designed which assigns different intensity levels of any color ex green to all the items, not the spectrum paint scale which gives all the different colors to the block).
How can I achieve this?
XYBlockRenderer ignores the outline properties inherited from the parent, AbstractRenderer. The drawItem() method simply sets the paint returned from the PaintScale, using the same color for both fill() and draw(). Some possible approaches would include these:
Override drawItem() and set a different paint after fill() but before draw(), perhaps using a brighter() or darker() color.
Use a PaintScale other than GrayPaintScale, such as LookupPaintScale or your own implementation. You can specify more distinctive colors at the problem end, perhaps using Color.getHSBColor() to vary the hue, saturation and/or brightness.
This example implements the PaintScale interface.
This example varies the hue in an XYLineAndShapeRenderer.
This example varies the saturation in a GanttRenderer.

create bar chart by accessing data from 2 .csv files

.csv file 1 data:
sampler_label,aggregate_report_count,average,aggregate_report_median,aggregate_report_90%_line
HTTP Request1, 750 ,26339 ,22644 , 40210
HTTP Request2, 750 ,8280 ,4781 ,21016
.csv file 2 data:
sampler_label,aggregate_report_count,average,aggregate_report_median,aggregate_report_90%_line
HTTP Request1, 350 ,2539 ,2224 , 48410
HTTP Request4, 350 ,8736 ,9285 ,38217
I want to display bar graph, which should depict values of average from both files for each sampler label.
Here there are 2 average values in each file. there may be n number of values. in bar graph i want to display values of file 1 and file 2 in one graph only.
Please help me....
public JFreeChart createBarChartFromCSV() {
csv csvReader = new csv();
List<String[]> csvData1 = null;
List<String[]> csvData2 = null;
int indexOfAverage1 = 0;
int indexOfAverage2 = 0;
csvData1 = csvReader.getDataFromCSV1(csv.CSVFILENAME1);
csvData2 = csvReader.getDataFromCSV2(csv.CSVFILENAME2);
for(String[] columnArray : csvData1)
for(int i = 0; i< columnArray.length; i++)
if(columnArray[i].equalsIgnoreCase("average")){
enter code here
indexOfAverage1 = i;
break;
}
if(indexOfAverage1 == 0){
System.err.println("Error retrieving data from CSV File1 !!");
System.exit(0);
}
for(String[] columnArray : csvData2)
for(int j = 0; j< columnArray.length; j++)
if(columnArray[j].equalsIgnoreCase("average")){
indexOfAverage2 = j;
break;
}
if(indexOfAverage2 == 0){
System.err.println("Error retrieving data from CSV File2 !!");
System.exit(0);
}
JFreeChart barChart = generateBarChart(csvData1,csvData2, indexOfAverage1,indexOfAverage2);
return barChart;
}
private JFreeChart generateBarChart(List<String[]> csvData1,List<String[]> csvData2, int columnIndex1, int columnIndex2){
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
final String YAXIS_NAME = csvData1.get(0)[columnIndex1]; //value returned is "average"
final String XAXIS_NAME = csvData1.get(0)[0]; //value returned is "sampler_label"
for(int i = 1; i < csvData1.size() - 1; i++){
long averageValue1 = Long.parseLong(csvData1.get(i)[columnIndex1]);
String columnKey1 = csvData1.get(i)[0];
dataSet.setValue(averageValue1, YAXIS_NAME, columnKey1); // plot the graph
}
for(int j = 1; j< csvData2.size() - 1; j++){
long averageValue2 = Long.parseLong(csvData2.get(j)[columnIndex2]);
String columnKey2 = csvData2.get(j)[0];
dataSet.setValue(averageValue2, YAXIS_NAME, columnKey2); // plot the graph
}
System.out.println("created");
JFreeChart chart = ChartFactory.createBarChart("Comparison between the average of 2 values", XAXIS_NAME, YAXIS_NAME, dataSet, PlotOrientation.VERTICAL, true, true, false);
final CategoryPlot plot = chart.getCategoryPlot();
chart.setBackgroundPaint(Color.white);
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
BarRenderer renderer=new BarRenderer();
System.out.print( "set the range axis to display integers only...");
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
System.out.print( "disable bar outlines...");
renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);
renderer.setMaximumBarWidth(0.10);
System.out.print( "set up gradient paints for series...");
final GradientPaint gp0 = new GradientPaint(
0.0f, 0.0f, Color.blue,
0.0f, 0.0f, Color.lightGray
);
final GradientPaint gp1 = new GradientPaint(
0.0f, 0.0f, Color.green,
0.0f, 0.0f, Color.lightGray
);
renderer.setSeriesPaint(0, gp0);
renderer.setSeriesPaint(1, gp1);
return chart;
}

JFreeChart - CategoryAxis step

I have a method for generating dataset:
private CategoryDataset createDataset(double[] arr,
String seriesName) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < arr.length; i++) {
dataset.addValue(arr[i], "mySeries", new Integer(i));
}
return dataset;
}
and create BarChart:
JFreeChart chart = ChartFactory.createBarChart(chartTitle,
xaxis, // domain axis label
yaxis, // range axis label
dataset, // data
orientation, // orientation
true, // include legend
true, // tooltips?
false // URLs?
);
Array of doubles hold histogram data, so there are 255 values.
When I display chart there are labels for
all values from 0 - 255 on x axis. I want display only labels for several indexes (for example: 0, 10, 20, 30). I saw that in RangeAxis there is setStandardTickUnits method. But in CategoryAxis:
CategoryAxis domainAxis = plot.getDomainAxis();
I didn't find this.
Any help?
You can try as follows,
NumberAxis vn = (NumberAxis) plot.getRangeAxis();
vn.setTickUnit(new NumberTickUnit(10d));
vn.setRange(0D, Math.ceil(factor * MAX_VALUE));
--that is you just need cast plot.getRangeAxis() to NumberAxis type.
I had same problem. I created new class implementing 'Comparable', and use it as last parameter in addValue(...). You can create something like
class MyCategory implements Comparable<MyCategory> {
Integer value;
String stValue;
MyCategory(int val) {
value = val;
stValue = val%10==0?""+val:"";}
public int compareTo(MyCategory key) { return value.compareTo(key.value); }
public String toString() { return stValue; }
}
And then instead of
dataset.addValue(arr[i], "mySeries", new Integer(i));
use
dataset.addValue(arr[i], "mySeries", new MyCategory(i));

Categories