1
0
mirror of https://github.com/mum4k/termdash.git synced 2025-04-28 13:48:51 +08:00
termdash/widgets/heatmap/heatmap.go

154 lines
5.1 KiB
Go
Raw Normal View History

2020-11-13 15:20:34 +08:00
// Copyright 2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package heatmap contains a widget that displays heat maps.
package heatmap
import (
"errors"
"image"
"sync"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/private/canvas"
"github.com/mum4k/termdash/terminal/terminalapi"
"github.com/mum4k/termdash/widgetapi"
"github.com/mum4k/termdash/widgets/heatmap/internal/axes"
)
2020-11-18 17:22:49 +08:00
// HeatMap draws heat map charts.
2020-11-17 21:19:50 +08:00
//
// Heatmap consists of several cells. Each cell represents a value.
2020-11-18 17:22:49 +08:00
// The larger the value, the darker the color of the cell (from white to black).
2020-11-17 21:19:50 +08:00
//
// The two dimensions of the values (cells) array are determined by the length of
2020-11-19 21:31:59 +08:00
// the xLabels and yLabels arrays respectively.
2020-11-17 21:19:50 +08:00
//
// HeatMap does not support mouse based zoom.
//
2020-11-13 15:20:34 +08:00
// Implements widgetapi.Widget. This object is thread-safe.
type HeatMap struct {
2020-11-17 21:19:50 +08:00
// values are the values in the heat map.
values [][]float64
2020-11-13 15:20:34 +08:00
2020-11-19 21:31:59 +08:00
// xLabels are the labels on the X axis in an increasing order.
xLabels []string
// yLabels are the labels on the Y axis in an increasing order.
yLabels []string
2020-11-13 15:20:34 +08:00
2020-11-20 20:17:24 +08:00
// minValue and maxValue are the Min and Max values in the values,
2020-11-18 16:01:09 +08:00
// which will be used to calculate the color of each cell.
2020-11-20 20:17:24 +08:00
minValue, maxValue float64
2020-11-18 16:01:09 +08:00
2020-11-23 10:57:07 +08:00
// lastWidth is the width of the canvas as of the last time when Draw was called.
lastWidth int
2020-11-13 15:20:34 +08:00
// opts are the provided options.
opts *options
// mu protects the HeatMap widget.
mu sync.RWMutex
}
2020-11-17 21:19:50 +08:00
// New returns a new HeatMap widget.
func New(opts ...Option) (*HeatMap, error) {
return nil, errors.New("not implemented")
2020-11-13 15:20:34 +08:00
}
2020-11-17 21:19:50 +08:00
// Values sets the values to be displayed by the HeatMap.
2020-11-20 20:17:24 +08:00
//
2020-11-17 21:19:50 +08:00
// Each value in values has a xLabel and a yLabel, which means
2020-11-23 10:57:07 +08:00
// len(yLabels) == len(values) and len(xLabels) == len(values[i]).
// But labels could be empty strings.
// When no labels are provided, labels will be "0", "1", "2"...
2020-11-20 20:17:24 +08:00
//
// Each call to Values overwrites any previously provided values.
2020-11-17 21:19:50 +08:00
// Provided options override values set when New() was called.
func (hp *HeatMap) Values(xLabels []string, yLabels []string, values [][]float64, opts ...Option) error {
return errors.New("not implemented")
2020-11-13 15:20:34 +08:00
}
2020-11-23 10:57:07 +08:00
// ClearXLabels clear the X labels.
func (hp *HeatMap) ClearXLabels() {
hp.xLabels = nil
}
// ClearYLabels clear the Y labels.
func (hp *HeatMap) ClearYLabels() {
hp.yLabels = nil
}
// ValueCapacity returns the number of values that can fit into the canvas.
// This is essentially the number of available cells on the canvas as observed
// on the last call to draw. Returns zero if draw wasn't called.
//
// Note that this capacity changes each time the terminal resizes, so there is
// no guarantee this remains the same next time Draw is called.
// Should be used as a hint only.
func (hp *HeatMap) ValueCapacity() int {
return 0
}
2020-11-13 15:20:34 +08:00
// axesDetails determines the details about the X and Y axes.
func (hp *HeatMap) axesDetails(cvs *canvas.Canvas) (*axes.XDetails, *axes.YDetails, error) {
2020-11-17 21:19:50 +08:00
return nil, nil, errors.New("not implemented")
2020-11-13 15:20:34 +08:00
}
2020-11-18 16:01:09 +08:00
// Draw draws cells, X labels and Y labels as HeatMap.
2020-11-13 15:20:34 +08:00
// Implements widgetapi.Widget.Draw.
func (hp *HeatMap) Draw(cvs *canvas.Canvas, meta *widgetapi.Meta) error {
2020-11-17 21:19:50 +08:00
return errors.New("not implemented")
2020-11-13 15:20:34 +08:00
}
2020-11-18 16:01:09 +08:00
// drawCells draws m*n cells (rectangles) representing the stored values.
// The height of each cell is 1 and the default width is 3.
2020-11-17 21:19:50 +08:00
func (hp *HeatMap) drawCells(cvs *canvas.Canvas, xd *axes.XDetails, yd *axes.YDetails) error {
return errors.New("not implemented")
2020-11-13 15:20:34 +08:00
}
2020-11-18 16:01:09 +08:00
// drawAxes draws X labels (under the cells) and Y Labels (on the left side of the cell).
func (hp *HeatMap) drawLabels(cvs *canvas.Canvas, xd *axes.XDetails, yd *axes.YDetails) error {
2020-11-17 21:19:50 +08:00
return errors.New("not implemented")
2020-11-13 15:20:34 +08:00
}
// minSize determines the minimum required size to draw HeatMap.
func (hp *HeatMap) minSize() image.Point {
2020-11-17 21:19:50 +08:00
return image.Point{}
2020-11-13 15:20:34 +08:00
}
2020-11-17 21:19:50 +08:00
// Keyboard input isn't supported on the HeatMap widget.
2020-11-13 15:20:34 +08:00
func (*HeatMap) Keyboard(k *terminalapi.Keyboard) error {
return errors.New("the HeatMap widget doesn't support keyboard events")
}
2020-11-17 21:19:50 +08:00
// Mouse input isn't supported on the HeatMap widget.
2020-11-13 15:20:34 +08:00
func (*HeatMap) Mouse(m *terminalapi.Mouse) error {
return errors.New("the HeatMap widget doesn't support mouse events")
}
// Options implements widgetapi.Widget.Options.
func (hp *HeatMap) Options() widgetapi.Options {
hp.mu.Lock()
defer hp.mu.Unlock()
return widgetapi.Options{}
}
2020-11-17 21:19:50 +08:00
// getCellColor returns the color of the cell according to its value.
2020-11-13 15:20:34 +08:00
// The larger the value, the darker the color.
2020-11-18 16:01:09 +08:00
// The color range is in Xterm color, from 232 to 255.
2020-11-17 21:19:50 +08:00
// Refer to https://jonasjacek.github.io/colors/.
func (hp *HeatMap) getCellColor(value float64) cell.Color {
return cell.ColorDefault
2020-11-13 15:20:34 +08:00
}