@@ -72,7 +72,7 @@ private JPanel createButtonPanel() {
7272 loadButton .addActionListener (e -> loadCsvFile ());
7373 toggleButton .addActionListener (e -> toggleDataView ());
7474 highlightBlanksButton .addActionListener (e -> highlightBlanks ());
75- heatmapButton .addActionListener (e -> toggleHeatmap (heatmapButton ));
75+ heatmapButton .addActionListener (e -> toggleHeatmap ());
7676 fontColorButton .addActionListener (e -> chooseFontColor ());
7777 insertRowButton .addActionListener (e -> insertRow ());
7878 deleteRowButton .addActionListener (e -> deleteRow ());
@@ -142,10 +142,8 @@ private void updateTableData(List<String[]> data) {
142142 for (String [] row : data ) {
143143 tableModel .addRow (row );
144144 }
145- if (isHeatmapEnabled ) {
146- applyHeatmap ();
147- } else if (isClassColorEnabled ) {
148- applyClassColorRenderer ();
145+ if (isHeatmapEnabled || isClassColorEnabled ) {
146+ applyCombinedRenderer ();
149147 } else {
150148 applyDefaultRenderer ();
151149 }
@@ -169,25 +167,39 @@ public Component getTableCellRendererComponent(JTable table, Object value, boole
169167 table .repaint ();
170168 }
171169
172- private void toggleHeatmap (JButton heatmapButton ) {
170+ private void toggleHeatmap () {
173171 isHeatmapEnabled = !isHeatmapEnabled ;
174- if (isHeatmapEnabled ) {
175- applyHeatmap ();
176- heatmapButton .setText ("H" );
177- heatmapButton .setToolTipText ("Hide Heatmap" );
178- } else {
179- applyDefaultRenderer ();
180- heatmapButton .setText ("M" );
181- heatmapButton .setToolTipText ("Show Heatmap" );
182- }
172+ applyCombinedRenderer ();
183173 dataHandler .updateStats (tableModel , statsTextArea );
184174 }
185175
186- private void applyHeatmap () {
176+ private void generateClassColors () {
177+ int classColumnIndex = tableModel .getColumnCount () - 1 ; // Assuming class column is the last one
178+ Map <String , Integer > classMap = new HashMap <>();
179+ int colorIndex = 0 ;
180+
181+ // Assign colors to each class
182+ for (int row = 0 ; row < tableModel .getRowCount (); row ++) {
183+ String className = (String ) tableModel .getValueAt (row , classColumnIndex );
184+ if (!classMap .containsKey (className )) {
185+ classMap .put (className , colorIndex ++);
186+ }
187+ }
188+
189+ // Create distinct colors for each class
190+ for (Map .Entry <String , Integer > entry : classMap .entrySet ()) {
191+ int value = entry .getValue ();
192+ Color color = new Color (Color .HSBtoRGB (value / (float ) classMap .size (), 1.0f , 1.0f ));
193+ classColors .put (entry .getKey (), color );
194+ }
195+ }
196+
197+ private void applyCombinedRenderer () {
187198 int numColumns = tableModel .getColumnCount ();
188199 double [] minValues = new double [numColumns ];
189200 double [] maxValues = new double [numColumns ];
190201 boolean [] isNumerical = new boolean [numColumns ];
202+ int classColumnIndex = tableModel .getColumnCount () - 1 ; // Assuming class column is the last one
191203
192204 // Initialize min and max values
193205 for (int i = 0 ; i < numColumns ; i ++) {
@@ -214,7 +226,17 @@ private void applyHeatmap() {
214226 public Component getTableCellRendererComponent (JTable table , Object value , boolean isSelected , boolean hasFocus , int row , int column ) {
215227 Component c = super .getTableCellRendererComponent (table , value , isSelected , hasFocus , row , column );
216228 int modelColumn = table .convertColumnIndexToModel (column ); // Get the model index of the column
217- if (value != null && !value .toString ().trim ().isEmpty () && isNumerical [modelColumn ]) {
229+
230+ // Apply class colors
231+ if (isClassColorEnabled && column == classColumnIndex ) {
232+ String className = (String ) value ;
233+ if (classColors .containsKey (className )) {
234+ c .setBackground (classColors .get (className ));
235+ } else {
236+ c .setBackground (Color .WHITE );
237+ }
238+ } else if (isHeatmapEnabled && value != null && !value .toString ().trim ().isEmpty () && isNumerical [modelColumn ]) {
239+ // Apply heatmap colors
218240 try {
219241 double val = Double .parseDouble (value .toString ());
220242 double normalizedValue = (val - minValues [modelColumn ]) / (maxValues [modelColumn ] - minValues [modelColumn ]);
@@ -226,6 +248,7 @@ public Component getTableCellRendererComponent(JTable table, Object value, boole
226248 } else {
227249 c .setBackground (Color .WHITE );
228250 }
251+
229252 c .setForeground (cellTextColor );
230253 return c ;
231254 }
@@ -242,48 +265,10 @@ private Color getColorForValue(double value) {
242265 table .repaint ();
243266 }
244267
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 ();
268+ private void toggleClassColors () {
269+ isClassColorEnabled = !isClassColorEnabled ;
270+ applyCombinedRenderer ();
271+ dataHandler .updateStats (tableModel , statsTextArea );
287272 }
288273
289274 private void applyDefaultRenderer () {
@@ -303,10 +288,8 @@ private void chooseFontColor() {
303288 Color newColor = JColorChooser .showDialog (this , "Choose Font Color" , cellTextColor );
304289 if (newColor != null ) {
305290 cellTextColor = newColor ;
306- if (isHeatmapEnabled ) {
307- applyHeatmap ();
308- } else if (isClassColorEnabled ) {
309- applyClassColorRenderer ();
291+ if (isHeatmapEnabled || isClassColorEnabled ) {
292+ applyCombinedRenderer ();
310293 } else {
311294 applyDefaultRenderer ();
312295 }
@@ -354,16 +337,6 @@ private void showParallelCoordinatesPlot() {
354337 plot .setVisible (true );
355338 }
356339
357- private void toggleClassColors () {
358- isClassColorEnabled = !isClassColorEnabled ;
359- if (isClassColorEnabled ) {
360- applyClassColorRenderer ();
361- } else {
362- applyDefaultRenderer ();
363- }
364- dataHandler .updateStats (tableModel , statsTextArea );
365- }
366-
367340 public static void main (String [] args ) {
368341 SwingUtilities .invokeLater (() -> {
369342 CsvViewer viewer = new CsvViewer ();
0 commit comments