LogStatus-table
状态栏表格,用LogStatus输出:列可以设置数值格式、涨跌色和单位,单元格可以合并、放按钮,多个表格可以分页显示。
示例
-
输出一个3行3列的表格。
javascriptfunction main() { var tbl = { type: "table", title: "标题", cols: ["列1", "列2", "列3"], rows: [ ["行1 列1", "行1 列2", "行1 列3"], ["行2 列1", "行2 列2", "行2 列3"], ["行3 列1", "行3 列2", "行3 列3"] ] } // 单独输出表格对象时自动加反引号 LogStatus(tbl) }pythondef main(): tbl = { "type": "table", "title": "标题", "cols": ["列1", "列2", "列3"], "rows": [ ["行1 列1", "行1 列2", "行1 列3"], ["行2 列1", "行2 列2", "行2 列3"], ["行3 列1", "行3 列2", "行3 列3"] ] } # 单独输出表格字典时自动加反引号 LogStatus(tbl)rustfn main() { let tbl = r#"{"type": "table", "title": "标题", "cols": ["列1", "列2", "列3"], "rows": [["行1 列1", "行1 列2", "行1 列3"], ["行2 列1", "行2 列2", "行2 列3"], ["行3 列1", "行3 列2", "行3 列3"]]}"#; // Rust 的字符串不会自动加反引号;反引号里的 JSON 要在一行里 LogStatus!(format!("`{}`", tbl.replace('\n', ""))); } -
最近10根日K线:列设置格式与涨跌色,单元格对象单独设置颜色。
javascriptfunction main() { var r = exchange.GetRecords(PERIOD_D1) if (!r || r.length < 11) return var rows = [] for (var i = r.length - 1; i >= r.length - 10; i--) { var chg = r[i].Close / r[i - 1].Close - 1 var note = Math.abs(chg) > 0.05 ? {value: "大幅波动", color: "warning"} : "" rows.push([_D(r[i].Time, "yyyy-MM-dd"), r[i].Close, chg, r[i].Volume, note]) } LogStatus({type: "table", title: "最近10天", rows: rows, cols: [ "日期", {title: "收盘", format: {precision: 2}, align: "right"}, {title: "涨跌幅", format: {percent: true, precision: 2}, color: "sign", align: "right"}, {title: "成交量", format: {compact: true}, align: "right"}, "备注" ]}) }pythondef main(): r = exchange.GetRecords(PERIOD_D1) if not r or len(r) < 11: return rows = [] for i in range(len(r) - 1, len(r) - 11, -1): chg = r[i]["Close"] / r[i - 1]["Close"] - 1 note = {"value": "大幅波动", "color": "warning"} if abs(chg) > 0.05 else "" rows.append([_D(r[i]["Time"] / 1000, "%Y-%m-%d"), r[i]["Close"], chg, r[i]["Volume"], note]) pct = {"percent": True, "precision": 2} LogStatus({"type": "table", "title": "最近10天", "rows": rows, "cols": [ "日期", {"title": "收盘", "format": {"precision": 2}, "align": "right"}, {"title": "涨跌幅", "format": pct, "color": "sign", "align": "right"}, {"title": "成交量", "format": {"compact": True}, "align": "right"}, "备注" ]})rustfn main() { let Ok(r) = exchange.GetRecords(None, PERIOD_D1, None) else { return }; if r.len() < 11 { return; } let rows: Vec<String> = (r.len() - 10..r.len()).rev().map(|i| { let chg = r[i].Close / r[i - 1].Close - 1.0; let big = r#"{"value": "大幅波动", "color": "warning"}"#; let note = if chg.abs() > 0.05 { big } else { r#""""# }; let date = &_D(r[i].Time)[..10]; format!(r#"["{}", {}, {}, {}, {}]"#, date, r[i].Close, chg, r[i].Volume, note) }).collect(); let table = format!(r#"{{"type": "table", "title": "最近10天", "rows": [{}], "cols": [ "日期", {{"title": "收盘", "format": {{"precision": 2}}, "align": "right"}}, {{"title": "涨跌幅", "format": {{"percent": true, "precision": 2}}, "color": "sign", "align": "right"}}, {{"title": "成交量", "format": {{"compact": true}}, "align": "right"}}, "备注"]}}"#, rows.join(",")); // 反引号里的 JSON 要在一行里 LogStatus!(format!("`{}`", table.replace('\n', ""))); } -
横向合并
LogStatus()函数画出的表格内的单元格。javascriptfunction main() { var table = { type: 'table', title: 'Position Operation', cols: ['Column 1', 'Column 2', 'Action'], rows: [ ['abc', 'def', {'type':'button', 'cmd': 'coverAll', 'name': 'Close'}] ] } var ticker = exchange.GetTicker() // 添加一行数据,第一个和第二个单元格合并,并且输出ticker变量在合并后的单元格内 table.rows.push([{body : JSON.stringify(ticker), colspan : 2}, "abc"]) LogStatus('`' + JSON.stringify(table) + '`') }pythonimport json def main(): table = { "type" : "table", "title" : "Position Operation", "cols" : ["Column 1", "Column 2", "Action"], "rows" : [ ["abc", "def", {"type": "button", "cmd": "coverAll", "name": "Close"}] ] } ticker = exchange.GetTicker() table["rows"].append([{"body": json.dumps(ticker), "colspan": 2}, "abc"]) LogStatus("`" + json.dumps(table) + "`")rustfn main() { let table_tpl = String::from(r#"{"type": "table", "title": "Position Operation", "cols": ["Column 1", "Column 2", "Action"], "rows": ["#) + r#"["abc", "def", {"type": "button", "cmd": "coverAll", "name": "Close"}],"# + r#"__ROW2__"# + r#"]}"#; let ticker = exchange.GetTicker(None).unwrap(); let json_ticker = format!( r#"{{"Buy": {}, "Sell": {}, "High": {}, "Low": {}, "Volume": {}, "Last": {}, "Time": {}}}"#, ticker.Buy, ticker.Sell, ticker.High, ticker.Low, ticker.Volume, ticker.Last, ticker.Time ); // 添加一行数据,第一个和第二个单元格合并,并且输出ticker数据在合并后的单元格内 // body为字符串(对应JS的JSON.stringify(ticker)),内部引号需转义后嵌入JSON let row2 = format!(r#"[{{"body": "{}", "colspan": 2}}, "abc"]"#, json_ticker.replace('"', "\\\"")); let table = table_tpl.replace("__ROW2__", &row2); LogStatus!(format!("`{}`", table)); } -
纵向合并
LogStatus()函数画出的表格内的单元格。javascriptfunction main() { var table = { type: 'table', title: 'Table Demo', cols: ['Column A', 'Column B', 'Column C'], rows: [ ['A1', 'B1', {'type':'button', 'cmd': 'coverAll', 'name': 'C1'}] ] } var ticker = exchange.GetTicker() var name = exchange.GetName() table.rows.push([{body : "A2 + B2:" + JSON.stringify(ticker), colspan : 2}, "C2"]) table.rows.push([{body : "A3 + A4 + A5:" + name, rowspan : 3}, "B3", "C3"]) // A4被上一行第一个单元格合并 table.rows.push(["B4", "C4"]) // A5被上一行第一个单元格合并 table.rows.push(["B5", "C5"]) table.rows.push(["A6", "B6", "C6"]) LogStatus('`' + JSON.stringify(table) + '`') }pythonimport json def main(): table = { "type" : "table", "title" : "Table Demo", "cols" : ["Column A", "Column B", "Column C"], "rows" : [ ["A1", "B1", {"type": "button", "cmd": "coverAll", "name": "C1"}] ] } ticker = exchange.GetTicker() name = exchange.GetName() table["rows"].append([{"body": "A2 + B2:" + json.dumps(ticker), "colspan": 2}, "C2"]) table["rows"].append([{"body": "A3 + A4 + A5:" + name, "rowspan": 3}, "B3", "C3"]) table["rows"].append(["B4", "C4"]) table["rows"].append(["B5", "C5"]) table["rows"].append(["A6", "B6", "C6"]) LogStatus("`" + json.dumps(table) + "`")rustfn main() { // 为了测试,代码简短易读,这里使用构造的数据 let json_ticker = r#"{"High": 0, "Low": 0, "Buy": 0, "Sell": 0, "Last": 0, "Time": 0, "Volume": 0}"#; let name = exchange.GetName(); let mut rows: Vec<String> = Vec::new(); rows.push(String::from(r#"["A1", "B1", {"type": "button", "cmd": "coverAll", "name": "C1"}]"#)); // body为字符串,内部的引号需要转义后嵌入JSON let body = format!("A2 + B2:{}", json_ticker).replace('"', "\\\""); rows.push(format!(r#"[{{"body": "{}", "colspan": 2}}, "C2"]"#, body)); rows.push(format!(r#"[{{"body": "A3 + A4 + A5:{}", "rowspan": 3}}, "B3", "C3"]"#, name)); // A4被上一行第一个单元格合并 rows.push(String::from(r#"["B4", "C4"]"#)); // A5被上一行第一个单元格合并 rows.push(String::from(r#"["B5", "C5"]"#)); rows.push(String::from(r#"["A6", "B6", "C6"]"#)); let table = format!(r#"{{"type": "table", "title": "Table Demo", "cols": ["Column A", "Column B", "Column C"], "rows": [{}]}}"#, rows.join(",")); LogStatus!(format!("`{}`", table)); } -
状态栏表格分页显示。
javascriptfunction main() { var table1 = {type: 'table', title: 'table1', cols: ['Column 1', 'Column 2'], rows: [ ['abc', 'def'], ['ABC', 'support color #ff0000']]} var table2 = {type: 'table', title: 'table2', cols: ['Column 1', 'Column 2'], rows: [ ['abc', 'def'], ['ABC', 'support color #ff0000']]} LogStatus('`' + JSON.stringify([table1, table2]) + '`') }pythonimport json def main(): table1 = {"type": "table", "title": "table1", "cols": ["Column 1", "Column 2"], "rows": [ ["abc", "def"], ["ABC", "support color #ff0000"]]} table2 = {"type": "table", "title": "table2", "cols": ["Column 1", "Column 2"], "rows": [ ["abc", "def"], ["ABC", "support color #ff0000"]]} LogStatus("`" + json.dumps([table1, table2]) + "`")rustfn main() { let table1 = r#"{"type": "table", "title": "table1", "cols": ["Column 1", "Column 2"], "rows": [["abc", "def"], ["ABC", "support color #ff0000"]]}"#; let table2 = r#"{"type": "table", "title": "table2", "cols": ["Column 1", "Column 2"], "rows": [["abc", "def"], ["ABC", "support color #ff0000"]]}"#; LogStatus!(format!("`[{},{}]`", table1, table2)); } -
除了可以分页显示表格,也可以多个表格自上而下排列显示。
javascriptfunction main(){ var tab1 = { type : "table", title : "Table 1", cols : ["1", "2"], rows : [] } var tab2 = { type : "table", title : "Table 2", cols : ["1", "2", "3"], rows : [] } var tab3 = { type : "table", title : "Table 3", cols : ["A", "B", "C"], rows : [] } tab1.rows.push(["jack", "lucy"]) tab2.rows.push(["A", "B", "C"]) tab3.rows.push(["A", "B", "C"]) LogStatus('`' + JSON.stringify(tab1) + '`\n' + '`' + JSON.stringify(tab2) + '`\n' + '`' + JSON.stringify(tab3) + '`') Log("exit") }pythonimport json def main(): tab1 = { "type": "table", "title": "Table 1", "cols": ["1", "2"], "rows": [] } tab2 = { "type": "table", "title": "Table 2", "cols": ["1", "2", "3"], "rows": [] } tab3 = { "type": "table", "title": "Table 3", "cols": ["A", "B", "C"], "rows": [] } tab1["rows"].append(["jack", "lucy"]) tab2["rows"].append(["A", "B", "C"]) tab3["rows"].append(["A", "B", "C"]) LogStatus("`" + json.dumps(tab1) + "`\n" + "`" + json.dumps(tab2) + "`\n" + "`" + json.dumps(tab3) + "`")rustfn main() { // Rust 中直接把行数据写入JSON字符串中 let tab1 = r#"{"type": "table", "title": "Table 1", "cols": ["1", "2"], "rows": [["jack", "lucy"]]}"#; let tab2 = r#"{"type": "table", "title": "Table 2", "cols": ["1", "2", "3"], "rows": [["A", "B", "C"]]}"#; let tab3 = r#"{"type": "table", "title": "Table 3", "cols": ["A", "B", "C"], "rows": [["A", "B", "C"]]}"#; LogStatus!(format!("`{}`\n`{}`\n`{}`", tab1, tab2, tab3)); Log!("exit"); } -
支持设置状态栏表格横向、纵向滚动模式。设置
scroll属性为auto,当状态栏表格纵向的行数超过20行时内容进行滚动显示,当横向的列数超出页面显示范围时进行横向滚动显示,使用scroll属性可以缓解实盘时状态栏中大量写入数据的卡顿问题。
参考以下测试例子:javascriptfunction main() { var tbl = { type : "table", title : "test scroll", scroll : "auto", cols : ["col 0", "col 1", "col 2", "col 3", "col 4", "col 5", "col 6", "col 7", "col 8", "col 9", "col 10", "col 11", "col 12", "col 13", "col 14", "col 15", "col 16", "col 17", "col 18", "col 19", "col 20"], rows : [] } for (var i = 1 ; i < 100 ; i++) { tbl.rows.push([i, "1," + i, "2," + i, "3," + i, "4," + i, "5," + i, "6," + i, "7," + i, "8," + i, "9," + i, "10," + i, "11," + i, "12," + i, "13," + i, "14," + i, "15," + i, "16," + i, "17," + i, "18," + i, "19," + i, "20," + i]) } LogStatus("`" + JSON.stringify(tbl) + "`") }pythonimport json def main(): tbl = { "type" : "table", "title" : "test scroll", "scroll" : "auto", "cols" : ["col 0", "col 1", "col 2", "col 3", "col 4", "col 5", "col 6", "col 7", "col 8", "col 9", "col 10", "col 11", "col 12", "col 13", "col 14", "col 15", "col 16", "col 17", "col 18", "col 19", "col 20"], "rows" : [] } for index in range(1, 100): i = str(index) tbl["rows"].append([i, "1," + i, "2," + i, "3," + i, "4," + i, "5," + i, "6," + i, "7," + i, "8," + i, "9," + i, "10," + i, "11," + i, "12," + i, "13," + i, "14," + i, "15," + i, "16," + i, "17," + i, "18," + i, "19," + i, "20," + i]) LogStatus("`" + json.dumps(tbl) + "`")rustfn main() { let tbl_tpl = String::from(r#"{"type": "table", "title": "test scroll", "scroll": "auto", "cols": ["#) + r#""col 0", "col 1", "col 2", "col 3", "col 4", "col 5", "col 6", "col 7", "col 8", "col 9", "col 10","# + r#""col 11", "col 12", "col 13", "col 14", "col 15", "col 16", "col 17", "col 18", "col 19", "col 20""# + r#"], "rows": [__ROWS__]}"#; let mut rows: Vec<String> = Vec::new(); for index in 1..100 { let i = index.to_string(); rows.push(format!( r#"[{}, "1,{}", "2,{}", "3,{}", "4,{}", "5,{}", "6,{}", "7,{}", "8,{}", "9,{}", "10,{}", "11,{}", "12,{}", "13,{}", "14,{}", "15,{}", "16,{}", "17,{}", "18,{}", "19,{}", "20,{}"]"#, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i )); } let tbl = tbl_tpl.replace("__ROWS__", &rows.join(",")); LogStatus!(format!("`{}`", tbl)); }
属性
| 名称 | 类型 | 描述 |
type | string | 必填,固定为 |
title | string | 表格标题。 |
cols | array | 必填。列标题数组:每项是字符串,或带 |
rows | array | 必填。二维数组,每行一个数组,元素与 |
scroll | string | 写 |
cols[].title | string | 列标题。 |
cols[].format | object | 本列数字的格式 |
cols[].color | string | 本列数字的颜色: |
cols[].unit | string | 本列数字后面的单位。 |
cols[].align | string | 对齐: |
rows[][].value | number / string | 格式化单元格的值。 |
rows[][].format | object | 该单元格的数值格式,优先于本列的设置。 |
rows[][].color | string | 该单元格的颜色,取值同 |
rows[][].unit | string | 该单元格的单位。 |
参考
备注
列定义只作用于该列的数字,字符串原样显示;单元格对象的设置优先于列定义。格式化后的文字仍按原始数值排序;表格里的搜索与导出CSV使用格式化后的文字。
单独输出表格时,JavaScript、Python直接传表格对象(自动加反引号),也可以写 LogStatus("`" + JSON.stringify(tbl) + "`");Rust写 format!("`{}`", table)。放进 grid、tabs 或含组件的数组时不用加反引号。
多个表格组成数组、用反引号包裹输出时显示为标签页;分行输出时自上而下排列。按钮放进单元格的写法见LogStatus-btnTypeTwo。