Skip to content

Commit

Permalink
[feature] addcol-histogram #2052
Browse files Browse the repository at this point in the history
  • Loading branch information
saulpw committed Oct 20, 2023
1 parent 0757606 commit d9d26f0
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions visidata/features/addcol_histogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from visidata import vd, Column, Sheet, asyncthread, Progress


class HistogramColumn(Column):
def calcValue(col, row):
histogram = col.sheet.options.disp_histogram
histolen = (col.width-2)*col.sourceCol.getTypedValue(row)//(col.sourceCol.largest-col.sourceCol.smallest)
return histogram*histolen


@Sheet.api
def addcol_histogram(sheet, col): #2052
newcol = HistogramColumn(col.name+'_histogram', sourceCol=col)
col.smallest = None
col.largest = None
sheet.calc_histogram_bounds(col)
return newcol


@Sheet.api
@asyncthread
def calc_histogram_bounds(sheet, col):
for row in Progress(sheet.rows):
v = col.getTypedValue(row)
if col.smallest is None or v < col.smallest:
col.smallest = v
if col.largest is None or v > col.largest:
col.largest = v


Sheet.addCommand('', 'addcol-histogram', 'addColumnAtCursor(addcol_histogram(cursorCol))', 'add column with histogram of current column')


vd.addMenuItems('Column > Add column > histogram')

0 comments on commit d9d26f0

Please sign in to comment.