ed768f7644e7de01fbfa83c1a8f17129ed3305d1
kate
  Tue Aug 1 10:13:34 2017 -0700
Minor code cleanup. refs #19872

diff --git src/hg/hgTracks/barChartTrack.c src/hg/hgTracks/barChartTrack.c
index 4de45a3..05c4b39 100644
--- src/hg/hgTracks/barChartTrack.c
+++ src/hg/hgTracks/barChartTrack.c
@@ -223,50 +223,51 @@
     if (expScore > maxScore)
         {
         maxScore = max(maxScore, expScore);
         maxNum = i;
         }
     totalScore += expScore;
     }
 // threshold to consider this item category specific -- a category contributes > threshold % to 
 // total level
 if (totalScore < 1 || maxScore <= (totalScore * threshold * .01))
     return -1;
 return maxNum;
 }
 
 double barChartMaxExpScore(struct track *tg, struct barChartItem *itemInfo)
-/* Return maximum expScore in a barChart */
+/* Determine maximum expScore in a barChart, set it and return it */
 {
 // use preset if available
 double maxScore = itemInfo->maxScore;
 if (maxScore > 0.0)
     return maxScore;
 
 // determine from bed
 maxScore = 0.0;
 struct bed *bed = (struct bed *)itemInfo->bed;
 int i;
 int expCount = bed->expCount;
 double expScore;
 for (i=0; i<expCount; i++)
     {
     if (!filterCategory(tg, getCategoryName(tg, i)))
         continue;
     expScore = bed->expScores[i];
     maxScore = max(maxScore, expScore);
     }
+itemInfo->maxScore = maxScore;
 return maxScore;
 }
 
 static Color barChartItemColor(struct track *tg, void *item, struct hvGfx *hvg)
 /* A bit of category-specific coloring in squish mode only, on bed item */
 {
 struct bed *bed = (struct bed *)item;
 int id = maxCategoryForItem(bed, SPECIFICITY_THRESHOLD);
 if (id < 0)
     return MG_BLACK;
 struct barChartTrack *extras = (struct barChartTrack *)tg->extraUiData;
 struct rgbColor color = extras->colors[id];
 return hvGfxFindColorIx(hvg, color.r, color.g, color.b);
 }
 
@@ -314,33 +315,35 @@
     AllocVar(itemInfo);
     itemInfo->bed = bed;
     slAddHead(&infoList, itemInfo);
     bed = bed->next;
     itemInfo->bed->next = NULL;
     itemInfo->maxScore = barChartMaxExpScore(tg, itemInfo);
     // for autoscale, determine maximum median score in window
     if (itemInfo->maxScore > extras->maxMedian)
         extras->maxMedian = itemInfo->maxScore;
     }
 /* determine graph heights */
 for (itemInfo = infoList; itemInfo != NULL; itemInfo = itemInfo->next)
     {
     itemInfo->height = barChartItemHeight(tg, itemInfo);
     }
-if (!extras->doAutoScale)
-    // maximum median score in entire dataset
-    extras->maxMedian = barChartUiMaxMedianScore(tdb);
+double  maxScore = barChartUiMaxMedianScore(tdb);
+if (!extras->doAutoScale && maxScore > extras->maxMedian)
+    // maximum median score in entire dataset.  
+    // If not set, using default, but might be too small for this dataset.
+    extras->maxMedian = maxScore;
 
 /* replace item list with wrapped beds */
 slReverse(&infoList);
 tg->items = infoList;
 }
 
 /***********************************************/
 /* Draw */
 
 /* Bargraph layouts for three window sizes */
 #define WIN_MAX_GRAPH 50000
 #define MAX_BAR_WIDTH 5
 #define MAX_GRAPH_PADDING 2
 
 #define WIN_MED_GRAPH 500000
@@ -434,31 +437,31 @@
     {
     useMax = maxView;
     if (val > maxView)
         useVal = maxView;
     }
 return valToHeight(useVal, useMax, extras->maxHeight, extras->doLogTransform);
 }
 
 static int barChartHeight(struct track *tg, struct barChartItem *itemInfo)
 /* Determine height in pixels of graph.  This will be the box for category with highest value */
 {
 struct barChartTrack *extras = (struct barChartTrack *)tg->extraUiData;
 double maxExp = barChartMaxExpScore(tg, itemInfo);
 double viewMax = (double)cartUsualIntClosestToHome(cart, tg->tdb, FALSE, 
                                 BAR_CHART_MAX_VIEW_LIMIT, BAR_CHART_MAX_VIEW_LIMIT_DEFAULT);
-double maxMedian = ((struct barChartTrack *)tg->extraUiData)->maxMedian;
+double maxMedian = extras->maxMedian;
 return valToClippedHeight(maxExp, maxMedian, viewMax, extras);
 }
 
 static void drawGraphBox(struct track *tg, struct barChartItem *itemInfo, struct hvGfx *hvg, int x, int y)
 /* Draw white background for graph */
 {
 Color lighterGray = MAKECOLOR_32(0xF3, 0xF3, 0xF3);
 int width = barChartWidth(tg, itemInfo);
 int height = barChartHeight(tg, itemInfo);
 hvGfxOutlinedBox(hvg, x, y-height, width, height, MG_WHITE, lighterGray);
 }
 
 static void drawGraphBase(struct track *tg, struct barChartItem *itemInfo, struct hvGfx *hvg, int x, int y)
 /* Draw faint line under graph to delineate extent when bars are missing (category w/ 0 value) */
 {
@@ -531,31 +534,31 @@
 
 if (!extras->noWhiteout)
     drawGraphBox(tg, itemInfo, hvg, keepX, yZero+1);
 
 struct rgbColor lineColor = {.r=0};
 int lineColorIx = hvGfxFindColorIx(hvg, lineColor.r, lineColor.g, lineColor.b);
 int barWidth = barChartBarWidth(tg);
 int graphPadding = barChartPadding();
 char *colorScheme = cartUsualStringClosestToHome(cart, tg->tdb, FALSE, BAR_CHART_COLORS, 
                         BAR_CHART_COLORS_DEFAULT);
 Color clipColor = MG_MAGENTA;
 
 // draw bar graph
 double viewMax = (double)cartUsualIntClosestToHome(cart, tg->tdb, FALSE, 
                                 BAR_CHART_MAX_VIEW_LIMIT, BAR_CHART_MAX_VIEW_LIMIT_DEFAULT);
-double maxMedian = ((struct barChartTrack *)tg->extraUiData)->maxMedian;
+double maxMedian = extras->maxMedian;
 int i;
 int expCount = bed->expCount;
 struct barChartCategory *categ;
 for (i=0, categ=extras->categories; i<expCount && categ != NULL; i++, categ=categ->next)
     {
     if (!filterCategory(tg, categ->name))
         continue;
     struct rgbColor fillColor = extras->colors[i];
     int fillColorIx = hvGfxFindColorIx(hvg, fillColor.r, fillColor.g, fillColor.b);
     double expScore = bed->expScores[i];
     int height = valToClippedHeight(expScore, maxMedian, viewMax, extras);
     if (graphPadding == 0 || sameString(colorScheme, BAR_CHART_COLORS_USER))
         hvGfxBox(hvg, x1, yZero-height+1, barWidth, height, fillColorIx);
     else
         hvGfxOutlinedBox(hvg, x1, yZero-height+1, barWidth, height, fillColorIx, lineColorIx);
@@ -691,31 +694,31 @@
 
 int labelWidth = mgFontStringWidth(tl.font, itemName);
 getItemX(start, end, &x1, &x2);
 if (x1-labelWidth <= insideX)
     labelWidth = 0;
 // map over label
 int itemHeight = itemInfo->height;
 mapBoxHc(hvg, itemStart, itemEnd, x1-labelWidth, y, labelWidth, itemHeight-3, 
                     tg->track, mapItemName, itemName);
 
 // add maps to category bars
 struct barChartCategory *categs = getCategories(tg);
 struct barChartCategory *categ = NULL;
 int barWidth = barChartBarWidth(tg);
 int padding = barChartPadding();
-double maxMedian = ((struct barChartTrack *)tg->extraUiData)->maxMedian;
+double maxMedian = extras->maxMedian;
 
 int graphX = barChartX(bed);
 if (graphX < 0)
     return;
 
 // x1 is at left of graph
 x1 = insideX + graphX;
 
 double viewMax = (double)cartUsualIntClosestToHome(cart, tg->tdb, FALSE, 
                                 BAR_CHART_MAX_VIEW_LIMIT, BAR_CHART_MAX_VIEW_LIMIT_DEFAULT);
 int i = 0;
 for (categ = categs; categ != NULL; categ = categ->next, i++)
     {
     if (!filterCategory(tg, categ->name))
         continue;