Skip to content

Commit b165a2d

Browse files
committed
Highlight tabular classes toggle
1 parent 183cb64 commit b165a2d

File tree

2 files changed

+71
-20
lines changed

2 files changed

+71
-20
lines changed

src/CsvViewer.java

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@
66
import javax.swing.table.TableRowSorter;
77
import java.awt.*;
88
import java.io.File;
9+
import java.util.HashMap;
910
import java.util.List;
11+
import java.util.Map;
1012

1113
public class CsvViewer extends JFrame {
1214
private JTable table;
1315
private ReorderableTableModel tableModel;
1416
private CsvDataHandler dataHandler;
1517
private boolean isNormalized = false;
1618
private boolean isHeatmapEnabled = false;
19+
private boolean isClassColorEnabled = false; // New flag for class column coloring
1720
private Color cellTextColor = Color.BLACK; // Default cell text color
1821
private JTextArea statsTextArea;
1922
private JButton toggleButton;
23+
private Map<String, Color> classColors = new HashMap<>(); // Store class colors
2024

2125
public CsvViewer() {
2226
setTitle("CSV Viewer");
@@ -63,6 +67,7 @@ private JPanel createButtonPanel() {
6367
JButton deleteRowButton = createButton("-", "Delete Row");
6468
JButton exportButton = createButton("E", "Export CSV");
6569
JButton parallelPlotButton = createButton("P", "Parallel Coordinates");
70+
JButton classColorButton = createButton("C", "Toggle Class Colors"); // New button for class coloring
6671

6772
loadButton.addActionListener(e -> loadCsvFile());
6873
toggleButton.addActionListener(e -> toggleDataView());
@@ -73,6 +78,7 @@ private JPanel createButtonPanel() {
7378
deleteRowButton.addActionListener(e -> deleteRow());
7479
exportButton.addActionListener(e -> exportCsvFile());
7580
parallelPlotButton.addActionListener(e -> showParallelCoordinatesPlot());
81+
classColorButton.addActionListener(e -> toggleClassColors()); // New action listener for class coloring
7682

7783
buttonPanel.add(loadButton);
7884
buttonPanel.add(toggleButton);
@@ -83,6 +89,7 @@ private JPanel createButtonPanel() {
8389
buttonPanel.add(deleteRowButton);
8490
buttonPanel.add(exportButton);
8591
buttonPanel.add(parallelPlotButton);
92+
buttonPanel.add(classColorButton); // Add new button to panel
8693

8794
return buttonPanel;
8895
}
@@ -104,7 +111,9 @@ private void loadCsvFile() {
104111
dataHandler.loadCsvData(filePath, tableModel, statsTextArea);
105112
isNormalized = false;
106113
isHeatmapEnabled = false; // Reset heatmap state when new CSV is loaded
114+
isClassColorEnabled = false; // Reset class color state when new CSV is loaded
107115
updateTableData(dataHandler.getOriginalData());
116+
generateClassColors(); // Generate class colors based on the loaded data
108117
}
109118
}
110119

@@ -135,6 +144,8 @@ private void updateTableData(List<String[]> data) {
135144
}
136145
if (isHeatmapEnabled) {
137146
applyHeatmap();
147+
} else if (isClassColorEnabled) {
148+
applyClassColorRenderer();
138149
} else {
139150
applyDefaultRenderer();
140151
}
@@ -231,6 +242,50 @@ private Color getColorForValue(double value) {
231242
table.repaint();
232243
}
233244

245+
private void generateClassColors() {
246+
int classColumnIndex = tableModel.getColumnCount() - 1; // Assuming class column is the last one
247+
Map<String, Integer> classMap = new HashMap<>();
248+
int colorIndex = 0;
249+
250+
// Assign colors to each class
251+
for (int row = 0; row < tableModel.getRowCount(); row++) {
252+
String className = (String) tableModel.getValueAt(row, classColumnIndex);
253+
if (!classMap.containsKey(className)) {
254+
classMap.put(className, colorIndex++);
255+
}
256+
}
257+
258+
// Create distinct colors for each class
259+
for (Map.Entry<String, Integer> entry : classMap.entrySet()) {
260+
int value = entry.getValue();
261+
Color color = new Color(Color.HSBtoRGB(value / (float) classMap.size(), 1.0f, 1.0f));
262+
classColors.put(entry.getKey(), color);
263+
}
264+
}
265+
266+
private void applyClassColorRenderer() {
267+
int classColumnIndex = tableModel.getColumnCount() - 1; // Assuming class column is the last one
268+
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
269+
@Override
270+
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
271+
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
272+
if (column == classColumnIndex) {
273+
String className = (String) value;
274+
if (classColors.containsKey(className)) {
275+
c.setBackground(classColors.get(className));
276+
} else {
277+
c.setBackground(Color.WHITE);
278+
}
279+
} else {
280+
c.setBackground(Color.WHITE);
281+
}
282+
c.setForeground(cellTextColor);
283+
return c;
284+
}
285+
});
286+
table.repaint();
287+
}
288+
234289
private void applyDefaultRenderer() {
235290
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
236291
@Override
@@ -250,6 +305,8 @@ private void chooseFontColor() {
250305
cellTextColor = newColor;
251306
if (isHeatmapEnabled) {
252307
applyHeatmap();
308+
} else if (isClassColorEnabled) {
309+
applyClassColorRenderer();
253310
} else {
254311
applyDefaultRenderer();
255312
}
@@ -293,10 +350,20 @@ private void showParallelCoordinatesPlot() {
293350
}
294351

295352
List<String[]> data = dataHandler.isDataEmpty() ? dataHandler.getOriginalData() : (isNormalized ? dataHandler.getNormalizedData() : dataHandler.getOriginalData());
296-
ParallelCoordinatesPlot plot = new ParallelCoordinatesPlot(data, columnNames, tableModel.getColumnCount() - 1);
353+
ParallelCoordinatesPlot plot = new ParallelCoordinatesPlot(data, columnNames, classColors, tableModel.getColumnCount() - 1);
297354
plot.setVisible(true);
298355
}
299356

357+
private void toggleClassColors() {
358+
isClassColorEnabled = !isClassColorEnabled;
359+
if (isClassColorEnabled) {
360+
applyClassColorRenderer();
361+
} else {
362+
applyDefaultRenderer();
363+
}
364+
dataHandler.updateStats(tableModel, statsTextArea);
365+
}
366+
300367
public static void main(String[] args) {
301368
SwingUtilities.invokeLater(() -> {
302369
CsvViewer viewer = new CsvViewer();

src/ParallelCoordinatesPlot.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package src;
2+
23
import org.jfree.chart.ChartFactory;
34
import org.jfree.chart.ChartPanel;
45
import org.jfree.chart.JFreeChart;
@@ -10,21 +11,20 @@
1011

1112
import javax.swing.*;
1213
import java.awt.*;
13-
import java.util.HashMap;
1414
import java.util.List;
1515
import java.util.Map;
1616

1717
public class ParallelCoordinatesPlot extends JFrame {
1818

1919
private Map<String, Color> classColors;
2020

21-
public ParallelCoordinatesPlot(List<String[]> data, String[] columnNames, int classColumnIndex) {
21+
public ParallelCoordinatesPlot(List<String[]> data, String[] columnNames, Map<String, Color> classColors, int classColumnIndex) {
2222
setTitle("Parallel Coordinates Plot");
2323
setSize(800, 600);
2424
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
2525
setLocationRelativeTo(null);
2626

27-
classColors = assignColorsToClasses(data, classColumnIndex);
27+
this.classColors = classColors;
2828
DefaultCategoryDataset dataset = createDataset(data, columnNames, classColumnIndex);
2929
JFreeChart chart = createChart(dataset, columnNames);
3030

@@ -77,20 +77,4 @@ public Paint getItemPaint(int row, int column) {
7777

7878
return chart;
7979
}
80-
81-
private Map<String, Color> assignColorsToClasses(List<String[]> data, int classColumnIndex) {
82-
Map<String, Color> colorMap = new HashMap<>();
83-
String[] colors = {"#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#FF00FF", "#00FFFF", "#000000"};
84-
85-
int colorIndex = 0;
86-
for (String[] row : data) {
87-
String className = row[classColumnIndex];
88-
if (!colorMap.containsKey(className)) {
89-
colorMap.put(className, Color.decode(colors[colorIndex % colors.length]));
90-
colorIndex++;
91-
}
92-
}
93-
94-
return colorMap;
95-
}
9680
}

0 commit comments

Comments
 (0)