i have problem with remove data from line chart. I wrote a program drawing graphs that after the action click on the button completes the chart data.
dataSeries1.getData().removeAll(); <- doesn't work.
Code:
NumberAxis xAxis = new NumberAxis();
xAxis.setLabel("Oś Y");
NumberAxis yAxis = new NumberAxis();
yAxis.setLabel("Oś X");
final LineChart lineChart = new LineChart(xAxis, yAxis);
final XYChart.Series dataSeries1 = new XYChart.Series();
lineChart.setCreateSymbols(false);
lineChart.getData().add(dataSeries1);
Button action:
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
int lewy = Integer.parseInt(kresLewy.getText());
int prawy = Integer.parseInt(kresPrawy.getText());
String rownanie = field.getText();
try {
String tekst = lewy + "; " + prawy + "; " + rownanie;
StringReader tekstReader = new StringReader(tekst);
parsery.interpreter.parser parser_obj
= new parsery.interpreter.parser(new parsery.interpreter.MyLexer(tekstReader));
Object result = parser_obj.parse().value;
String sWynik = result.toString();
ZmiennaX zX = new ZmiennaX();
ArrayList<Double> xArr = new ArrayList<Double>();
for (double i = lewy; i <= prawy + 0.001; i = i + zX.getDokladnosc()) // +0.001 dla bledow zaokraglenia
{
xArr.add(zX.round2(i));
}
String sX = xArr.toString();
String wartosciX = sX.substring(1, sX.length() - 1);
String wartosciY = sWynik.substring(1, sWynik.length() - 1);
String XbezSpacji = wartosciX.replace(" ", "");
String YbezSpacji = wartosciY.replace(" ", "");
String[] splitX = XbezSpacji.split(",");
String[] splitY = YbezSpacji.split(",");
dataSeries1.getData().removeAll();
for(int i=0; i<splitX.length; i++){
double x = Double.parseDouble(splitX[i]);
double y = Double.parseDouble(splitY[i]);
dataSeries1.getData().add(new XYChart.Data(x, y));
}
} catch (Exception e) {
System.out.println("Podczs obliczenia wystapil blad. (" + e.getMessage() + ")");
} catch (Error error) {
System.out.println("Podczs obliczenia wystapil blad. (" + error.getMessage() + ")");
}
}
});
Can anyone help me to remove data after drawing a new chart?
removeAll requires elements to be removed passed in its parameter. Since you provided none - nothing gets removed:
dataSeries1.getData().removeAll();
You want to use clear() instead in the line above.
public void clear()
Removes all of the elements from this list (optional operation). The list will be empty after this call returns.
You can use Collections.singleton to remove all data:
dataSeries1.getData().removeAll(Collections.singleton(barChart.getData().setAll()));
See Oracle documentation for Collections.singleton https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html
Related
I don't know if I'm using the correct way of filtering a barchart, or even if my code is properly written.
But I have problem, when I open my dashboard, I have 3 charts, one of the piecharts can filter the barchart, and depending on which was selected, the barchart only shows the piechart selected, otherwise shows all.
My problem is when I filter those, I'm getting the correct filter, but I can't get the labels to appear, they just appear when I got only one, for example if I have 1Yellow, 15 Reds, its only a bar to show, but it only shows the label for the yellow one, but if yellow2 and 15 reds, it doesn't show any of them.
It should be a easy fix, I just can't see where is the error.
ArrayList<Integer> colors_to_add_type = new ArrayList<>();
ArrayList<Integer> colors_to_add_type_MINOR = new ArrayList<>();
ArrayList<Integer> colors_to_add_type_MEDIUM = new ArrayList<>();
ArrayList<Integer> colors_to_add_type_MAJOR = new ArrayList<>();
ArrayList<Integer> colors_to_add_type_OTHER = new ArrayList<>();
Iterator<String> defects_colors_keys;
defects_colors_keys = colors_type.keys();
while (defects_colors_keys.hasNext()){
String key = defects_colors_keys.next();
Log.e("COLOR_KEY", "onResponse: " + key );
int to_add = Color.parseColor(colors_type.getString(key));
int to_add_MINOR = Color.parseColor("#00FF00");
int to_add_MEDIUM = Color.parseColor("#FFFF00");
int to_add_MAJOR = Color.parseColor("#FF0000");
int to_add_OTHER = Color.parseColor("#800080");
Log.e("COLOR_NAME", "onResponse: " + colors_type.getString(key));
colors_to_add_type.add(to_add);
colors_to_add_type_MAJOR.add(to_add_MAJOR);
colors_to_add_type_MEDIUM.add(to_add_MEDIUM);
colors_to_add_type_MINOR.add(to_add_MINOR);
colors_to_add_type_OTHER.add(to_add_OTHER);
}
List<LegendEntry> legends_type = new ArrayList<>();
List<LegendEntry> legends_type_MINOR = new ArrayList<>();
List<LegendEntry> legends_type_MEDIUM = new ArrayList<>();
List<LegendEntry> legends_type_MAJOR = new ArrayList<>();
List<LegendEntry> legends_type_OTHER = new ArrayList<>();
type_of_defects = by_type.keys();
int c = 0;
if(!map_defects_by_type_count.isEmpty()){
map_defects_by_type_count.clear();
}
Log.e("DEFECTS", defects_list.toString());
Log.e("DEFECTS",String.valueOf(defects_list.size()));
while(type_of_defects.hasNext()) {
String key = type_of_defects.next();
Log.e("DEFECTS_type", key.toString());
map_defects_by_type_count.put(key, by_type.getInt(key));
map_get_defect_type.put(c, key);
LegendEntry legendEntry = new LegendEntry();
legendEntry.label = key;
legendEntry.formColor = colors_to_add_type.get(c);
legends_type.add(legendEntry);
arr_by_type.add(new BarEntry((float)c + 1, (int)by_type.getInt(key), (String)key));
c++;
for (int i = 0; i < defects_list.size(); i++){
if (defects_list.get(i).getStringProperty("defect").equals(key)){
if (defects_list.get(i).getStringProperty("criticality").equals("MAJOR")){
arr_by_type_MAJOR.add(new BarEntry((float)c +1, (int)by_type.getInt(key), (String)key));
legends_type_MAJOR.add(legendEntry);
break;
}
if (defects_list.get(i).getStringProperty("criticality").equals("MEDIUM")){
arr_by_type_MEDIUM.add(new BarEntry((float)c +1 , (int)by_type.getInt(key), (String)key));
legends_type_MEDIUM.add(legendEntry);
break;
}
if (defects_list.get(i).getStringProperty("criticality").equals("MINOR")){
arr_by_type_MINOR.add(new BarEntry((float)c +1, (int)by_type.getInt(key), (String)key));
legends_type_MINOR.add(legendEntry);
break;
}
if (defects_list.get(i).getStringProperty("criticality").equals("OTHER")){
arr_by_type_OTHER.add(new BarEntry((float)c +1, (int)by_type.getInt(key), (String)key));
legends_type_OTHER.add(legendEntry);
break;
}
}
}
}
TOTAL_DEFECTS = TOTAL_BY_CRIT;
if(TOTAL_BY_CRIT != 0){
if(MAJOR != 0){
major_percent = (float) MAJOR / TOTAL_BY_CRIT;
}
if(MEDIUM != 0){
medium_percent = (float) MEDIUM / TOTAL_BY_CRIT;
}
if(MINOR != 0){
minor_percent = (float) MINOR / TOTAL_BY_CRIT;
}
if(OTHER != 0){
other_percent = (float) OTHER / TOTAL_BY_CRIT;
}
}
TOTAL_SOURCE = TOTAL_H_M;
if(TOTAL_H_M != 0){
if(HUMAN != 0){
human_percent = (float) HUMAN / TOTAL_H_M;
}
if(MACHINE != 0){
machine_percent = (float) MACHINE / TOTAL_H_M;
}
}
arr_by_defect.add(new PieEntry(MAJOR, "MAJOR"));
arr_by_defect.add(new PieEntry(MEDIUM, "MEDIUM"));
arr_by_defect.add(new PieEntry(MINOR, "MINOR"));
arr_by_defect.add(new PieEntry(OTHER, "OTHER"));
arr_by_source.add(new PieEntry(HUMAN, "HUMAN"));
arr_by_source.add(new PieEntry(MACHINE, "MACHINE"));
dataSet_by_defect = new PieDataSet(arr_by_defect, "");
dataSet_by_source = new PieDataSet(arr_by_source, "");
dataSet_by_type = new BarDataSet(arr_by_type, "");
dataSet_by_type_MAJOR = new BarDataSet(arr_by_type_MAJOR, "");
dataSet_by_type_MEDIUM = new BarDataSet(arr_by_type_MEDIUM, "");
dataSet_by_type_MINOR = new BarDataSet(arr_by_type_MINOR, "");
dataSet_by_type_OTHER = new BarDataSet(arr_by_type_OTHER, "");
//GET THE COLORS
String colorMajor = colors.getString("MAJOR");
String colorMedium = colors.getString("MEDIUM");
String colorMinor = colors.getString("MINOR");
String colorOther = colors.getString("OTHER");
String colorHuman = human_machine_colors.getString("human");
String colorMachine = human_machine_colors.getString("machine");
final int[] sliceColors = {Color.parseColor(colorMajor), Color.parseColor(colorMedium), Color.parseColor(colorMinor), Color.parseColor(colorOther)};
ArrayList<Integer> colorsSet = new ArrayList<>();
for (int color : sliceColors){
colorsSet.add(color);
}
Log.e("COLORS_TO ADD", "MAJOR" + colorMajor);
Log.e("COLORS_TO ADD", "MEDIUM" + colorMedium);
Log.e("COLORS_TO ADD", "MINOR" + colorMinor);
Log.e("COLORS_TO ADD", "OTHER" + colorOther);
dataSet_by_defect.setColors(colorsSet);
final int[] sliceColors_hm = {Color.parseColor(colorHuman), Color.parseColor(colorMachine)};
ArrayList<Integer> colorsSet_hm = new ArrayList<>();
for (int color : sliceColors_hm){
colorsSet_hm.add(color);
}
dataSet_by_source.setColors(colorsSet_hm);
dataSet_by_type.setColors(colors_to_add_type);
dataSet_by_type_MINOR.setColors(colors_to_add_type_MINOR);
dataSet_by_type_MEDIUM.setColors(colors_to_add_type_MEDIUM);
dataSet_by_type_MAJOR.setColors(colors_to_add_type_MAJOR);
dataSet_by_type_OTHER.setColors(colors_to_add_type_OTHER);
dataSet_by_type.setAxisDependency(YAxis.AxisDependency.RIGHT);
dataSet_by_type_MINOR.setAxisDependency(YAxis.AxisDependency.RIGHT);
dataSet_by_type_MEDIUM.setAxisDependency(YAxis.AxisDependency.RIGHT);
dataSet_by_type_MAJOR.setAxisDependency(YAxis.AxisDependency.RIGHT);
dataSet_by_type_OTHER.setAxisDependency(YAxis.AxisDependency.RIGHT);
final PieData data_defect = new PieData(dataSet_by_defect);
final PieData data_source = new PieData(dataSet_by_source);
final BarData data_type = new BarData(dataSet_by_type);
final BarData data_type_MINOR = new BarData(dataSet_by_type_MINOR);
final BarData data_type_MEDIUM = new BarData(dataSet_by_type_MEDIUM);
final BarData data_type_MAJOR = new BarData(dataSet_by_type_MAJOR);
final BarData data_type_OTHER = new BarData(dataSet_by_type_OTHER);
data_type.setDrawValues(true);
data_type_MAJOR.setDrawValues(true);
data_type_MEDIUM.setDrawValues(true);
data_type_MINOR.setDrawValues(true);
data_type_OTHER.setDrawValues(true);
data_defect.setDrawValues(false);
data_source.setDrawValues(false);
//data_type.setBarWidth(.75f);
piechart_defects.setDrawHoleEnabled(true);
piechart_defects.setHoleColor(Color.TRANSPARENT);
piechart_defects.setDrawEntryLabels(false);
piechart_defects.setDrawMarkers(false);
piechart_defects.setCenterText(String.valueOf(TOTAL_DEFECTS) + " TOTAL");
piechart_defects.setCenterTextSize(10);
piechart_defects.getDescription().setEnabled(false);
Legend l_defect = piechart_defects.getLegend();
l_defect.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l_defect.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l_defect.setOrientation(Legend.LegendOrientation.VERTICAL);
l_defect.setDrawInside(false);
l_defect.setEnabled(true);
l_defect.setFormSize(4f);
l_defect.setTextColor(Color.WHITE);
l_defect.setTextSize(.5f);
piechart_source.setDrawHoleEnabled(true);
piechart_source.setDrawEntryLabels(false);
piechart_source.setHoleColor(Color.TRANSPARENT);
piechart_source.setDrawMarkers(false);
piechart_source.setCenterText(String.valueOf(TOTAL_SOURCE) + " TOTAL");
piechart_source.setCenterTextSize(10);
piechart_source.getDescription().setEnabled(false);
piechart_source.setTransparentCircleRadius(10f);
Legend l_source = piechart_source.getLegend();
l_source.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l_source.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l_source.setOrientation(Legend.LegendOrientation.VERTICAL);
l_source.setDrawInside(false);
l_source.setEnabled(true);
l_source.setFormSize(4f);
l_source.setTextColor(Color.WHITE);
l_source.setTextSize(.5f);
dataSet_by_type.setValueTextColor(Color.WHITE);
dataSet_by_type_MAJOR.setValueTextColor(Color.WHITE);
dataSet_by_type_MEDIUM.setValueTextColor(Color.WHITE);
dataSet_by_type_MINOR.setValueTextColor(Color.WHITE);
dataSet_by_type_OTHER.setValueTextColor(Color.WHITE);
dataSet_by_type.setValueTextSize(7f);
dataSet_by_type_MINOR.setValueTextSize(7f);
dataSet_by_type_MEDIUM.setValueTextSize(7f);
dataSet_by_type_MAJOR.setValueTextSize(7f);
dataSet_by_type_OTHER.setValueTextSize(7f);
data_type.setBarWidth(.5f);
data_type.setValueFormatter(new ValueFormatter() {
#Override
public String getBarLabel(BarEntry barEntry) {
int barEntryY = (int) barEntry.getY();
return (String)barEntry.getData() + " " + "(" + String.valueOf(barEntryY) + ")";
}
});
Legend l_type_MAJOR = barchart_type.getLegend();
l_type_MAJOR.setCustom(legends_type_MAJOR);
l_type_MAJOR.setWordWrapEnabled(true);
l_type_MAJOR.setDrawInside(true);
data_type_MAJOR.setValueFormatter(new ValueFormatter() {
#Override
public String getBarLabel(BarEntry barEntry) {
int barEntryY = (int) barEntry.getY();
return (String)barEntry.getData() + " " + "(" + String.valueOf(barEntryY) + ")";
}
});
data_type_MAJOR.setBarWidth(.5f);
Legend l_type_OTHER = barchart_type.getLegend();
l_type_OTHER.setCustom(legends_type_OTHER);
l_type_OTHER.setWordWrapEnabled(true);
l_type_OTHER.setDrawInside(true);
data_type_OTHER.setValueFormatter(new ValueFormatter() {
#Override
public String getBarLabel(BarEntry barEntry) {
int barEntryY = (int) barEntry.getY();
return (String)barEntry.getData() + " " + "(" + String.valueOf(barEntryY) + ")";
}
});
data_type_OTHER.setBarWidth(.5f);
Legend l_type_MINOR = barchart_type.getLegend();
l_type_MINOR.setCustom(legends_type_MINOR);
l_type_MINOR.setWordWrapEnabled(true);
l_type_MINOR.setDrawInside(true);
data_type_MINOR.setValueFormatter(new ValueFormatter() {
#Override
public String getBarLabel(BarEntry barEntry) {
int barEntryY = (int) barEntry.getY();
return (String)barEntry.getData() + " " + "(" + String.valueOf(barEntryY) + ")";
}
});
Legend l_type_MEDIUM = barchart_type.getLegend();
l_type_MEDIUM.setCustom(legends_type_MEDIUM);
l_type_MEDIUM.setWordWrapEnabled(true);
l_type_MEDIUM.setDrawInside(true);
data_type_MEDIUM.setValueFormatter(new ValueFormatter() {
#Override
public String getBarLabel(BarEntry barEntry) {
int barEntryY = (int) barEntry.getY();
return (String)barEntry.getData() + " " + "(" + String.valueOf(barEntryY) + ")";
}
});
barchart_type.setRenderer(new MyRenderer(barchart_type, barchart_type.getAnimator(), barchart_type.getViewPortHandler()));
Legend l_type = barchart_type.getLegend();
l_type.setCustom(legends_type);
l_type.setWordWrapEnabled(true);
l_type.setDrawInside(true);
piechart_defects.setData(data_defect);
piechart_defects.invalidate();
piechart_source.setData(data_source);
piechart_source.invalidate();
barchart_type.getAxisLeft().setEnabled(false);
barchart_type.setScaleEnabled(false);
barchart_type.getAxisRight().setEnabled(false);
barchart_type.getAxisRight().setDrawGridLines(false);
barchart_type.getXAxis().setDrawLabels(false);
barchart_type.getXAxis().setDrawGridLines(false);
barchart_type.getDescription().setEnabled(false);
barchart_type.getLegend().setEnabled(false);
barchart_type.setBorderColor(Color.BLACK);
barchart_type.setDrawBorders(false);
barchart_type.setData(data_type);
barchart_type.setMinimumHeight(arr_by_type.size()*250);
barchart_type.invalidate();
Log.e("INFO BAR", data_type.toString());
piechart_defects.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
#Override
public void onValueSelected(Entry e, Highlight h) {
String value = String.valueOf((int)h.getY());
String toShow = "";
if(h.getX() == 0){
toShow = " MAJOR \n" + value;
chartSelectedDefect = "MAJOR";
barchart_type.setData(data_type_MAJOR);
}else if(h.getX() == 1){
toShow = " MEDIUM \n" + value;
chartSelectedDefect = "MEDIUM";
barchart_type.setData(data_type_MEDIUM);
barchart_type.invalidate();
}else if(h.getX() == 2){
toShow = " MINOR \n" + value ;
chartSelectedDefect = "MINOR";
barchart_type.setData(data_type_MINOR);
}else if(h.getX() == 3){
toShow = " OTHER \n" + value;
chartSelectedDefect = "OTHER";
barchart_type.setData(data_type_OTHER);
How it appears when I show all:
I am trying to get a learning curve for an automated weka experiment. I currently have the following java code.
public static void EvaluateModel(AbstractClassifier cl, String datapath, String outfile) throws Exception {
Experiment exp = new Experiment();
ClassifierSplitEvaluator se = new ClassifierSplitEvaluator();
se.setClassifier(cl);
Classifier sec = ((ClassifierSplitEvaluator) se).getClassifier();
CrossValidationResultProducer cvrp = new CrossValidationResultProducer();
cvrp.setNumFolds(10);
cvrp.setSplitEvaluator(se);
PropertyNode[] propertyPath = new PropertyNode[2];
try {
propertyPath[0] = new PropertyNode(
se,
new PropertyDescriptor("splitEvaluator",
CrossValidationResultProducer.class),
CrossValidationResultProducer.class);
propertyPath[1] = new PropertyNode(sec,
new PropertyDescriptor("classifier", se.getClass()),
se.getClass());
} catch (IntrospectionException e) {
e.printStackTrace();
}
exp.setResultProducer(cvrp);
exp.setPropertyPath(propertyPath);
exp.setPropertyArray(new Classifier[]{cl});
DefaultListModel model = new DefaultListModel();
model.addElement(new File(datapath));
exp.setDatasets(model);
InstancesResultListener irl = new InstancesResultListener();
irl.setOutputFile(new File(outfile));
exp.setResultListener(irl);
System.out.println("Initializing...");
exp.initialize();
System.out.println("Running...");
exp.runExperiment();
System.out.println("Finishing...");
exp.postProcess();
System.out.println("Evaluating...");
PairedTTester tester = new PairedCorrectedTTester();
FileReader reader = new FileReader(irl.getOutputFile());
Instances result = new Instances(reader);
tester.setInstances(result);
tester.setSortColumn(-1);
tester.setRunColumn(result.attribute("Key_Run").index());
tester.setFoldColumn(result.attribute("Key_Fold").index());
tester.setDatasetKeyColumns(
new Range(
""
+ (result.attribute("Key_Dataset").index() + 1)));
tester.setResultsetKeyColumns(
new Range(
""
+ (result.attribute("Key_Scheme").index() + 1)
+ ","
+ (result.attribute("Key_Scheme_options").index() + 1)
+ ","
+ (result.attribute("Key_Scheme_version_ID").index() + 1)));
tester.setResultMatrix(new ResultMatrixPlainText());
tester.setDisplayedResultsets(null);
tester.setSignificanceLevel(0.05);
tester.setShowStdDevs(true);
// fill result matrix (but discarding the output)
tester.multiResultsetFull(0, result.attribute("Percent_correct").index());
// output results for reach dataset
System.out.println("\nResult:");
ResultMatrix matrix = tester.getResultMatrix();
for (int i = 0; i < matrix.getColCount(); i++) {
System.out.println(matrix.getColName(i));
System.out.println(" Perc. correct: " + matrix.getMean(i, 0));
System.out.println(" StdDev: " + matrix.getStdDev(i, 0));
}
}
What I would like to do is either save or display the learning curve in this method. I cannot find info for how to do this programmatically.
I have the following method
private void passStingR(StringBuilder retVal) throws BadLocationException, Exception {
int getinitialscrollpos;
getinitialscrollpos = getinitialscrollPosition(scrollMEL);
//1. get chartPanel Bounds
Rectangle2D xy = chartPanel.getScreenDataArea();
Rectangle bounds = chartPanel.getBounds();
int horz = (int) bounds.getX();//need to get the correct x and y
int vertz = (int) bounds.getY();
int width1 = (int) bounds.getWidth();
int height1 = (int) bounds.getHeight();
System.out.println(horz + " " + vertz + " " + width1 + " " +height1);//get positioning data
/////////////////////////////////
Document docR = null;
docR = loadXMLFromString(retVal.toString());//pull in the XML data into a new doc
populate1R(docR);
tableMEL.getTableHeader().setReorderingAllowed(false);//prevent user from changing column order now at refresh level
SimpleDateFormat time_formatterR = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String current_time_strR = time_formatterR.format(System.currentTimeMillis());
updatetFieldDG.setText(current_time_strR);
middlePanel.add(scrollMEL);
scrollMEL.getVerticalScrollBar().setValue(getinitialscrollpos);
System.out.println("End" + getinitialscrollpos);
getTableData(tableMEL);
head(tableMEL);
createGraphGUI();
//2.the problem lies here as i would expect the chartPanel.setBounds to restore the position that i got at 1.
chartPanel.setBounds(horz,vertz,width1,height1);//xywidthheight
System.out.println(chartPanel.getBounds());//set the positioning items as per the start
////////////
}
The issue is that when my data is refreshed i lose the zoom positioning.
I have therefore tried to get the chartPanel bounds at the start of the method and then try to apply it at the end of the method - however the zoom positioning just keeps on reverting back to the default position which is just the whole graph. Please can someone help?
I worked this out:
private void passStingR(StringBuilder retVal) throws BadLocationException, Exception {
int getinitialscrollpos;
getinitialscrollpos = getinitialscrollPosition(scrollMEL);
try {
CategoryPlot plot = (CategoryPlot) chartPanel.getChart().getPlot();//get plot of graph
//System.out.print("Plot Type" + plot);
ValueAxis rangeAxis = plot.getRangeAxis();//get range of axis of graph
System.out.print("Range " + rangeAxis);//get range lower and upper as an array
map.put(1, rangeAxis);
} catch (Exception e) {
System.out.print("Error has occured");
} finally {
////refresh data////
Document docR = null;
docR = loadXMLFromString(retVal.toString());//pull in the XML data into a new doc
populate1R(docR);
tableMEL.getTableHeader().setReorderingAllowed(false);//prevent user from changing column order now at refresh level
SimpleDateFormat time_formatterR = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String current_time_strR = time_formatterR.format(System.currentTimeMillis());
updatetFieldDG.setText(current_time_strR);
middlePanel.add(scrollMEL);
scrollMEL.getVerticalScrollBar().setValue(getinitialscrollpos);
System.out.println("End" + getinitialscrollpos);
getTableData(tableMEL);
head(tableMEL);
createGraphGUI();
if (map.get(1) != null) {
System.out.print("get map" + map.get(1));
CategoryPlot plot = (CategoryPlot) chartPanel.getChart().getPlot();
plot.setRangeAxis(map.get(1));//get range of axis of graph
}
}
}
I am trying to detect faces and crop the face part in rectangular Image. I have done the face detection Part, but still not finding any help about how to crop the face part. Please have a look on my code..!
public class FaceDetect extends Activity {
private MyImageView mIV;
private Bitmap mFaceBitmap;
private int mFaceWidth = 200;
private int mFaceHeight = 200;
int cropXinit = 0;
int cropYint = 0;
int cropXend = 0;
int cropYend = 0;
Bitmap cropedBitmap;
Bitmap b;
private static final int MAX_FACES = 1;
private static String TAG = "FaceDetect";
private static boolean DEBUG = false;
protected static final int GUIUPDATE_SETFACE = 999;
protected Handler mHandler = new Handler() {
// #Override
public void handleMessage(Message msg) {
mIV.invalidate();
super.handleMessage(msg);
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mIV = new MyImageView(this);
setContentView(mIV, new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
// load the photo
b = ChooseActivity.bitmap;
mFaceBitmap = b.copy(Bitmap.Config.RGB_565, true);
b.recycle();
mFaceWidth = mFaceBitmap.getWidth();
mFaceHeight = mFaceBitmap.getHeight();
mIV.setImageBitmap(mFaceBitmap);
mIV.invalidate();
setFace();
}
public void setFace() {
FaceDetector fd;
FaceDetector.Face[] faces = new FaceDetector.Face[MAX_FACES];
PointF eyescenter = new PointF();
float eyesdist = 0.0f;
int[] fpx = null;
int[] fpy = null;
int count = 0;
try {
fd = new FaceDetector(mFaceWidth, mFaceHeight, MAX_FACES);
count = fd.findFaces(mFaceBitmap, faces);
} catch (Exception e) {
Log.e(TAG, "setFace(): " + e.toString());
return;
}
// check if we detect any faces
if (count > 0) {
fpx = new int[count * 2];
fpy = new int[count * 2];
for (int i = 0; i < count; i++) {
try {
faces[i].getMidPoint(eyescenter);
eyesdist = faces[i].eyesDistance();
// set up left eye location
fpx[2 * i] = (int) (eyescenter.x - eyesdist / 2);
fpy[2 * i] = (int) eyescenter.y;
// set up right eye location
fpx[2 * i + 1] = (int) (eyescenter.x + eyesdist / 2);
fpy[2 * i + 1] = (int) eyescenter.y;
if (DEBUG)
Log.e(TAG,
"setFace(): face "
+ i
+ ": confidence = "
+ faces[i].confidence()
+ ", eyes distance = "
+ faces[i].eyesDistance()
+ ", pose = ("
+ faces[i]
.pose(FaceDetector.Face.EULER_X)
+ ","
+ faces[i]
.pose(FaceDetector.Face.EULER_Y)
+ ","
+ faces[i]
.pose(FaceDetector.Face.EULER_Z)
+ ")" + ", eyes midpoint = ("
+ eyescenter.x + "," + eyescenter.y
+ ")");
} catch (Exception e) {
Log.e(TAG, "setFace(): face " + i + ": " + e.toString());
}
}
}
mIV.setDisplayPoints(fpx, fpy, count * 2, 1);
// if(eyescenter.x -eyesdist >= 0)
// {
// cropXinit = (int) (eyescenter.x -eyesdist) ;
// }
// else
// {
// cropXinit = 0;
// }
// if(eyescenter.x +eyesdist <= mFaceWidth)
// {
// cropXend = (int) (eyescenter.x +eyesdist) ;
// }
// else
// {
// cropXend = mFaceWidth;
// }
// if(eyescenter.y +eyesdist*2 <= mFaceHeight)
// {
// cropYend = (int) (eyescenter.y +eyesdist*2) ;
// }
// else
// {
// cropYend = mFaceHeight;
// }
// if(eyescenter.y -eyesdist >= 0)
// {
// cropYint = (int) (eyescenter.y -eyesdist) ;
// }
// else
// {
// cropYint = 0;
// }
// mIV.setImageBitmap(Bitmap.createBitmap(mFaceBitmap,cropXinit,cropYint,cropXend,cropYend));
}
}
createBitmap(Bitmap source, int x, int y, int width, int height) receives a start X and start Y, and a width and height value, not an end X and end Y. If you change your commented-out code to this it should work:
mIV.setImageBitmap(Bitmap.createBitmap(mFaceBitmap,cropXinit,cropYint,cropXend-cropXinit,cropYend-cropYinit));
try {
final List<String> ar = new ArrayList<String>();
final PRIvariable pri = new PRIvariable();
final BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream("C:/cdr2.csv")));
while (reader.ready()) {
final String line = reader.readLine();
final String[] values = line.split(",");
pri.dateText = values[2] + " " + values[4];
pri.count = pri.count + 1;
pri.sum = pri.sum + Integer.parseInt(values[7]);
System.out.println(pri.dateText + " " + pri.sum + " " + pri.count);
ar.add(pri);
}
final String[] columnNames = { "Date", "TOTAL", "COUNTS" };
final String[][] cells = new String[ar.size()][3];
for (int i = 0; i < ar.size(); i++) {
cells[i][0] = ((PRIvariable) ar.get(i)).dateText;
cells[i][1] = "" + ((PRIvariable) ar.get(i)).sum;
cells[i][2] = "" + ((PRIvariable) ar.get(i)).count;
}
table = new JTable(cells, columnNames);
table.setSize(400, 400);
table.setVisible(true);
final JScrollPane js = new JScrollPane();
js.setViewportView(table);
js.setSize(400, 400);
js.setVisible(true);
add(js, java.awt.BorderLayout.CENTER);
} catch (final Exception e) {
System.out.println(e);
}
This is my code. Here i want to Read text file and put that data to Jtable. But in this code it shows every row of the Jtable filled with same data that contain in arraylist(ar) last row. ( i think there is problem in my arraylist). How can i solve this......
The problem is with the variable pri. It has to be created inside the while loop.
Like this
String line = null;
while ((line = reader.readLine()) != null) {
PRIvariable pri = new PRIvariable();
String[] values = line.split(",");
pri.dateText = values[2] + " " + values[4];
pri.count = pri.count + 1;
pri.sum = pri.sum + Integer.parseInt(values[7]);
System.out.println(pri.dateText + " " + pri.sum + " " + pri.count);
ar.add(pri);
}
In your code your want to create a separate instance of PRIvariable for every line in the file, but you are creating only once instance of PRIvariable at the beginning then you are always using that instance by overriding the previous value.