66import javax .swing .table .TableRowSorter ;
77import java .awt .*;
88import java .io .File ;
9+ import java .util .HashMap ;
910import java .util .List ;
11+ import java .util .Map ;
1012
1113public 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 ();
0 commit comments